bash git clone https://github.com/your_username/osgi_enroute_authorization_simple_provider.git bash cd osgi_enroute_authorization_simple_provider bash mvn install bash karaf@root> install -s mvn:org.osgi/enroute/auth/simple/1.0.0-SNAPSHOT bash felix-start.sh bash karaf@root> run package org.osgi.enroute.authorization.simple; public interface Authorizer { boolean isAuthorized(String principal, String resource); } package org.osgi.enroute.authorization.simple; public class SimpleAuthorizer implements Authorizer { @Override public boolean isAuthorized(String principal, String resource) { return true; } } package org.osgi.enroute.authorization.simple; import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceTracker; public class AuthorizerTracker extends ServiceTracker { private BundleContext bundleContext; private ServiceTracker serviceTracker; public AuthorizerTracker(BundleContext bundleContext) { super(bundleContext); } @Override public void setServiceTracker(ServiceTracker serviceTracker) { this.serviceTracker = serviceTracker; } @Override public void addedService(ServiceReference serviceReference) { super.addedService(serviceReference); Authorizer authorizer = (Authorizer) serviceReference.getService(); if (authorizer != null) { bundleContext.registerService(Authorizer.class.getName(), authorizer, null); } } @Override public void removedService(ServiceReference serviceReference) { super.removedService(serviceReference); } } package org.osgi.enroute.authorization.simple; import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceReference; public class ExampleBundle { private BundleContext bundleContext; private Authorizer authorizer; public ExampleBundle(BundleContext bundleContext) { this.bundleContext = bundleContext; } public void initialize() { ServiceReference authorizerRef = bundleContext.getServiceReference(Authorizer.class.getName()); authorizer = (Authorizer) bundleContext.getService(authorizerRef); } public boolean isAuthorized(String principal, String resource) { if (authorizer != null) { return authorizer.isAuthorized(principal, resource); } return false; } }


上一篇:
下一篇:
切换中文