Added:
/trunk/dependency-shot-dynamic/src/main/java/cx/ath/mancel01/dependencyshot/dynamic/DependencyShotActivator.java
/trunk/dependency-shot-dynamic/src/main/java/cx/ath/mancel01/dependencyshot/dynamic/config
/trunk/dependency-shot-dynamic/src/main/java/cx/ath/mancel01/dependencyshot/dynamic/config/DynamicBinder.java
/trunk/dependency-shot-dynamic/src/main/java/cx/ath/mancel01/dependencyshot/dynamic/config/DynamicProvider.java
/trunk/dependency-shot-dynamic/src/main/java/cx/ath/mancel01/dependencyshot/dynamic/config/OSGiConfigurator.java
/trunk/dependency-shot-dynamic/src/main/java/cx/ath/mancel01/dependencyshot/dynamic/config/Publish.java
/trunk/dependency-shot-dynamic/src/main/java/cx/ath/mancel01/dependencyshot/dynamic/event
/trunk/dependency-shot-dynamic/src/main/java/cx/ath/mancel01/dependencyshot/dynamic/event/AbstractServiceEvent.java
/trunk/dependency-shot-dynamic/src/main/java/cx/ath/mancel01/dependencyshot/dynamic/event/BundleContainerInitialized.java
/trunk/dependency-shot-dynamic/src/main/java/cx/ath/mancel01/dependencyshot/dynamic/event/BundleContainerShutdown.java
/trunk/dependency-shot-dynamic/src/main/java/cx/ath/mancel01/dependencyshot/dynamic/event/ServiceArrival.java
/trunk/dependency-shot-dynamic/src/main/java/cx/ath/mancel01/dependencyshot/dynamic/event/ServiceChanged.java
/trunk/dependency-shot-dynamic/src/main/java/cx/ath/mancel01/dependencyshot/dynamic/event/ServiceDeparture.java
/trunk/dependency-shot-dynamic/src/main/java/cx/ath/mancel01/dependencyshot/dynamic/registry
/trunk/dependency-shot-dynamic/src/main/java/cx/ath/mancel01/dependencyshot/dynamic/registry/OSGiServiceRegistryImpl.java
/trunk/dependency-shot-dynamic/src/main/java/cx/ath/mancel01/dependencyshot/dynamic/registry/ServiceRegistry.java
/trunk/dependency-shot-dynamic/src/main/java/cx/ath/mancel01/dependencyshot/dynamic/registry/ServiceRegistryImpl.java
/trunk/dependency-shot-dynamic/src/main/java/cx/ath/mancel01/dependencyshot/dynamic/registry/ServiceRegistryProvider.java
/trunk/dependency-shot-dynamic/src/main/java/cx/ath/mancel01/dependencyshot/dynamic/scope
/trunk/dependency-shot-dynamic/src/main/java/cx/ath/mancel01/dependencyshot/dynamic/scope/DynamicImplementation.java
/trunk/dependency-shot-dynamic/src/main/java/cx/ath/mancel01/dependencyshot/dynamic/scope/DynamicProxy.java
/trunk/dependency-shot-dynamic/src/main/java/cx/ath/mancel01/dependencyshot/dynamic/scope/DynamicScope.java
/trunk/dependency-shot-dynamic/src/main/java/cx/ath/mancel01/dependencyshot/dynamic/scope/ProxyHelper.java
Deleted:
/trunk/dependency-shot-dynamic/src/main/java/cx/ath/mancel01/dependencyshot/dynamic/DynamicBinder.java
/trunk/dependency-shot-dynamic/src/main/java/cx/ath/mancel01/dependencyshot/dynamic/DynamicImplementation.java
/trunk/dependency-shot-dynamic/src/main/java/cx/ath/mancel01/dependencyshot/dynamic/DynamicProvider.java
/trunk/dependency-shot-dynamic/src/main/java/cx/ath/mancel01/dependencyshot/dynamic/DynamicProxy.java
/trunk/dependency-shot-dynamic/src/main/java/cx/ath/mancel01/dependencyshot/dynamic/DynamicScope.java
/trunk/dependency-shot-dynamic/src/main/java/cx/ath/mancel01/dependencyshot/dynamic/Internal.java
/trunk/dependency-shot-dynamic/src/main/java/cx/ath/mancel01/dependencyshot/dynamic/OSGiConfigurator.java
/trunk/dependency-shot-dynamic/src/main/java/cx/ath/mancel01/dependencyshot/dynamic/ProxyHelper.java
/trunk/dependency-shot-dynamic/src/main/java/cx/ath/mancel01/dependencyshot/dynamic/ServiceRegistry.java
/trunk/dependency-shot-dynamic/src/main/java/cx/ath/mancel01/dependencyshot/dynamic/ServiceRegistryImpl.java
Modified:
/trunk/dependency-shot-dynamic/src/main/resources/META-INF/services/cx.ath.mancel01.dependencyshot.spi.BindingsProvider
/trunk/dependency-shot-dynamic/src/main/resources/META-INF/services/cx.ath.mancel01.dependencyshot.spi.ConfigurationHandler
/trunk/dependency-shot-dynamic/src/main/resources/META-INF/services/cx.ath.mancel01.dependencyshot.spi.CustomScopeHandler
/trunk/dependency-shot-dynamic/src/test/java/cx/ath/mancel01/dependencyshot/dynamic/DynamicTest.java
/trunk/dependency-shot-dynamic/src/test/resources/META-INF/services/cx.ath.mancel01.dependencyshot.spi.CustomScopeHandler
=======================================
--- /dev/null
+++
/trunk/dependency-shot-dynamic/src/main/java/cx/ath/mancel01/dependencyshot/dynamic/DependencyShotActivator.java
Thu Apr 7 09:13:58 2011
@@ -0,0 +1,186 @@
+/*
+ * Copyright 2011 mathieuancelin.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * under the License.
+ */
+
+package cx.ath.mancel01.dependencyshot.dynamic;
+
+import cx.ath.mancel01.dependencyshot.DependencyShot;
+import cx.ath.mancel01.dependencyshot.api.DSBinder;
+import cx.ath.mancel01.dependencyshot.dynamic.config.OSGiConfigurator;
+import cx.ath.mancel01.dependencyshot.dynamic.config.Publish;
+import cx.ath.mancel01.dependencyshot.dynamic.event.AbstractServiceEvent;
+import
cx.ath.mancel01.dependencyshot.dynamic.event.BundleContainerInitialized;
+import
cx.ath.mancel01.dependencyshot.dynamic.event.BundleContainerShutdown;
+import cx.ath.mancel01.dependencyshot.dynamic.event.ServiceArrival;
+import cx.ath.mancel01.dependencyshot.dynamic.event.ServiceChanged;
+import cx.ath.mancel01.dependencyshot.dynamic.event.ServiceDeparture;
+import cx.ath.mancel01.dependencyshot.dynamic.registry.ServiceRegistry;
+import cx.ath.mancel01.dependencyshot.event.InjectorStoppedEvent;
+import cx.ath.mancel01.dependencyshot.injection.InjectorImpl;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Dictionary;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleActivator;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.BundleEvent;
+import org.osgi.framework.BundleListener;
+import org.osgi.framework.ServiceEvent;
+import org.osgi.framework.ServiceListener;
+import org.osgi.framework.ServiceReference;
+
+/**
+ *
+ * @author mathieuancelin
+ */
+public class DependencyShotActivator implements BundleActivator,
+ BundleListener, ServiceListener{
+
+ public static final String DYNAMIC_BINDER = "Dynamic-Binder";
+ public static final String DEPENDENCY_SHOT_ENABLED
= "Dependency-Shot-Enabled";
+
+ private Map<Long, InjectorImpl> injectors = new HashMap<Long,
InjectorImpl>();
+ private BundleContext context;
+
+ @Override
+ public void start(BundleContext bc) throws Exception {
+ this.context = bc;
+ for (Bundle bundle : bc.getBundles()) {
+ if (Bundle.ACTIVE == bundle.getState()) {
+ startContainer(bundle);
+ }
+ }
+ bc.addBundleListener(this);
+ bc.addServiceListener(this);
+ }
+
+ @Override
+ public void stop(BundleContext bc) throws Exception {
+ for (Long bundleId : injectors.keySet()) {
+ stopContainer(bc.getBundle(bundleId));
+ }
+ }
+
+ @Override
+ public void bundleChanged(BundleEvent be) {
+ switch (be.getType()) {
+ case BundleEvent.STARTED:
+ startContainer(be.getBundle());
+ break;
+ case BundleEvent.STOPPED:
+ stopContainer(be.getBundle());
+ break;
+ }
+ for (InjectorImpl injector : injectors.values()) {
+ injector.fireAsync(be);
+ }
+ }
+
+ @Override
+ public void serviceChanged(ServiceEvent se) {
+ ServiceReference ref = se.getServiceReference();
+ AbstractServiceEvent serviceEvent = null;
+ switch (se.getType()) {
+ case ServiceEvent.MODIFIED:
+ serviceEvent =
+ new ServiceChanged(ref, context);
+ break;
+ case ServiceEvent.REGISTERED:
+ serviceEvent =
+ new ServiceArrival(ref, context);
+ break;
+ case ServiceEvent.UNREGISTERING:
+ serviceEvent =
+ new ServiceDeparture(ref, context);
+ break;
+ }
+ if (serviceEvent != null) {
+ for (InjectorImpl injector : injectors.values()) {
+ injector.fireAsync(serviceEvent);
+ }
+ }
+ }
+
+ private void stopContainer(Bundle bundle) {
+ if (injectors.containsKey(bundle.getBundleId())) {
+ System.out.println("Stopping Dependency-Shot container for
bundle " + bundle.getSymbolicName());
+ InjectorImpl injector = injectors.get(bundle.getBundleId());
+ injector.fire(new
BundleContainerShutdown(bundle.getBundleContext()));
+ injector.triggerLifecycleDestroyCallbacks();
+ injector.getInstance(InjectorStoppedEvent.class).fire();
+ }
+ }
+
+ private void startContainer(Bundle bundle) {
+ Dictionary dict = bundle.getHeaders();
+ if (dict.get(DEPENDENCY_SHOT_ENABLED) != null) {
+ String binderName = (String) dict.get(DYNAMIC_BINDER);
+ Class binderClazz = null;
+ try {
+ binderClazz = bundle.loadClass(binderName);
+ } catch (ClassNotFoundException ex) {
+ }
+ DSBinder binder = null;
+ if (binderClazz != null) {
+ try {
+ binder = (DSBinder) binderClazz.newInstance();
+ } catch (Exception ex) {
+ }
+ }
+ System.out.println("Starting Dependency-Shot container for
bundle " + bundle.getSymbolicName());
+ InjectorImpl injector = null;
+ if (binder != null) {
+ injector = DependencyShot
+ .configurator(OSGiConfigurator.class)
+ .context(bundle.getBundleContext())
+ .withBinder(binder)
+ .getInjector();
+ } else {
+ injector = DependencyShot
+ .configurator(OSGiConfigurator.class)
+ .context(bundle.getBundleContext())
+ .getInjector();
+ }
+ List<String> discoveredClasses = new ArrayList<String>();
+ Enumeration beanClasses = bundle.findEntries("", "*.class",
true);
+ if (beanClasses != null) {
+ while (beanClasses.hasMoreElements()) {
+ URL url = (URL) beanClasses.nextElement();
+ String clazz =
url.getFile().substring(1).replace("/", ".").replace(".class", "");
+ discoveredClasses.add(clazz);
+ }
+ }
+ ServiceRegistry reg =
injector.getInstance(ServiceRegistry.class);
+ for (String clazzName : discoveredClasses) {
+ try {
+ Class<?> clazz = bundle.loadClass(clazzName);
+ if (clazz.isAnnotationPresent(Publish.class)) {
+ for (Class<?> itf : clazz.getInterfaces()) {
+ reg.registerService(itf, clazz);
+ }
+ }
+ } catch (ClassNotFoundException ex) {
+ }
+ }
+ injector.fire(new
BundleContainerInitialized(bundle.getBundleContext()));
+ injectors.put(bundle.getBundleId(), injector);
+ }
+ }
+}
=======================================
--- /dev/null
+++
/trunk/dependency-shot-dynamic/src/main/java/cx/ath/mancel01/dependencyshot/dynamic/config/DynamicBinder.java
Thu Apr 7 09:13:58 2011
@@ -0,0 +1,101 @@
+/*
+ * Copyright 2010 mathieu.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * under the License.
+ */
+
+package cx.ath.mancel01.dependencyshot.dynamic.config;
+
+import cx.ath.mancel01.dependencyshot.api.DSBinder;
+import cx.ath.mancel01.dependencyshot.dynamic.scope.DynamicImplementation;
+import cx.ath.mancel01.dependencyshot.exceptions.DSIllegalStateException;
+import cx.ath.mancel01.dependencyshot.graph.Binder;
+import cx.ath.mancel01.dependencyshot.graph.BinderAccessor;
+import cx.ath.mancel01.dependencyshot.graph.Binding;
+import cx.ath.mancel01.dependencyshot.injection.InjectorImpl;
+import java.lang.reflect.Field;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.List;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+/**
+ *
+ * @author mathieu
+ */
+public abstract class DynamicBinder extends Binder {
+
+ private static final Logger logger =
Logger.getLogger(DynamicBinder.class.getName());
+
+ private List<DSBinder> delegateBinders = new ArrayList<DSBinder>();
+
+ private boolean isOSGi = false;
+
+ public DynamicBinder() {
+ }
+
+ public DynamicBinder(DSBinder... binders) {
+ delegateBinders.addAll(Arrays.asList(binders));
+ }
+
+ @Override
+ public final void configureBindings() {
+ for (DSBinder binder: delegateBinders) {
+ BinderAccessor.setInjector((Binder) binder, (InjectorImpl)
injector());
+ binder.configureBindings();
+ BinderAccessor.configureLastBinding((Binder) binder);
+ }
+ configure();
+ }
+
+ public abstract void configure();
+
+ public void bindDynamically(Class<?> from) {
+ try {
+ bind(from);
+ Field fTo = Binder.class.getDeclaredField("to");
+ fTo.setAccessible(true);
+ fTo.set(this, DynamicImplementation.class);
+ fTo.setAccessible(false);
+ } catch (Exception ex) {
+ logger.log(Level.SEVERE, "Unable to bind dynamically the
class " + from.getName(), ex);
+ }
+ validate(from);
+ }
+
+ private void validate(Class<?> dynamic) {
+ ArrayList<Class<?>> errors = new ArrayList<Class<?>>();
+ Collection<Binding<?>> registered = this.getBindings().keySet();
+ for (Binding binding : registered) {
+ if (binding.getFrom() != null && binding.getTo() != null
+ && dynamic.equals(binding.getFrom())
+
&& !binding.getTo().equals(DynamicImplementation.class)) {
+ errors.add(binding.getFrom());
+ }
+ }
+ if (!errors.isEmpty()) {
+ StringBuilder builder = new StringBuilder();
+ builder.append("The following classes are both declared for
dynamic injection and static injection.");
+ builder.append("\nIt can't append !\n");
+ for (Class<?> clazz : errors) {
+ builder.append(" ");
+ builder.append(clazz.getName());
+ builder.append("\n");
+ }
+ throw new DSIllegalStateException(builder.toString());
+ }
+ }
+}
=======================================
--- /dev/null
+++
/trunk/dependency-shot-dynamic/src/main/java/cx/ath/mancel01/dependencyshot/dynamic/config/DynamicProvider.java
Thu Apr 7 09:13:58 2011
@@ -0,0 +1,53 @@
+/*
+ * Copyright 2009-2010 Mathieu ANCELIN.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * under the License.
+ */
+
+package cx.ath.mancel01.dependencyshot.dynamic.config;
+
+import cx.ath.mancel01.dependencyshot.dynamic.registry.ServiceRegistry;
+import
cx.ath.mancel01.dependencyshot.dynamic.registry.ServiceRegistryProvider;
+import cx.ath.mancel01.dependencyshot.graph.Binding;
+import cx.ath.mancel01.dependencyshot.graph.BindingBuilder;
+import cx.ath.mancel01.dependencyshot.injection.InjectorImpl;
+import cx.ath.mancel01.dependencyshot.spi.BindingsProvider;
+import java.util.ArrayList;
+import java.util.Collection;
+
+/**
+ *
+ * @author Mathieu ANCELIN
+ */
+public class DynamicProvider extends BindingsProvider {
+
+ @Override
+ public Collection<Binding> getProvidedBindings(InjectorImpl injector) {
+
+ ArrayList<Binding> bindings = new ArrayList<Binding>();
+
+ bindings.add(BindingBuilder
+ .prepareBindingThat()
+ .bind(ServiceRegistry.class)
+ .providedBy(new ServiceRegistryProvider())
+ .build());
+ bindings.add(BindingBuilder
+ .prepareBindingThat()
+ .bind(ServiceRegistryProvider.OSGiEnvHolder.class)
+ .build());
+
+ return bindings;
+ }
+
+}
=======================================
--- /dev/null
+++
/trunk/dependency-shot-dynamic/src/main/java/cx/ath/mancel01/dependencyshot/dynamic/config/OSGiConfigurator.java
Thu Apr 7 09:13:58 2011
@@ -0,0 +1,77 @@
+/*
+ * Copyright 2009-2010 Mathieu ANCELIN.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * under the License.
+ */
+package cx.ath.mancel01.dependencyshot.dynamic.config;
+
+import cx.ath.mancel01.dependencyshot.DependencyShot;
+import cx.ath.mancel01.dependencyshot.api.DSBinder;
+import cx.ath.mancel01.dependencyshot.api.Stage;
+import
cx.ath.mancel01.dependencyshot.dynamic.registry.OSGiServiceRegistryImpl;
+import cx.ath.mancel01.dependencyshot.dynamic.registry.ServiceRegistry;
+import
cx.ath.mancel01.dependencyshot.dynamic.registry.ServiceRegistryProvider;
+import cx.ath.mancel01.dependencyshot.graph.Binder;
+import cx.ath.mancel01.dependencyshot.injection.InjectorImpl;
+import cx.ath.mancel01.dependencyshot.spi.ConfigurationHandler;
+import org.osgi.framework.BundleContext;
+
+/**
+ *
+ * @author Mathieu ANCELIN
+ */
+public class OSGiConfigurator extends ConfigurationHandler {
+
+ private BundleContext context;
+ private Binder binder;
+ private ServiceRegistry registry;
+
+ @Override
+ public InjectorImpl getInjector(Stage stage) {
+ InjectorImpl injector = null;
+ if (context == null) {
+ throw new IllegalStateException("You must set the current
bundle context.");
+ }
+ if (binder == null) {
+ // TODO :
+ injector = DependencyShot.getInjector();
+ } else {
+ injector = DependencyShot.getInjector(binder);
+ }
+ ServiceRegistryProvider.OSGiEnvHolder holder =
+
injector.getInstance(ServiceRegistryProvider.OSGiEnvHolder.class);
+ holder.setOsgi(true);
+ registry = injector.getInstance(ServiceRegistry.class);
+ if (registry == null) {
+ throw new IllegalStateException("Injection for bundle context
failed.");
+ }
+ ((OSGiServiceRegistryImpl) registry).setContext(context);
+ return injector;
+ }
+
+ @Override
+ public boolean isAutoEnabled() {
+ return false;
+ }
+
+ public OSGiConfigurator withBinder(DSBinder binder) {
+ this.binder = (Binder) binder;
+ return this;
+ }
+
+ public OSGiConfigurator context(BundleContext context) {
+ this.context = context;
+ return this;
+ }
+}
=======================================
--- /dev/null
+++
/trunk/dependency-shot-dynamic/src/main/java/cx/ath/mancel01/dependencyshot/dynamic/config/Publish.java
Thu Apr 7 09:13:58 2011
@@ -0,0 +1,34 @@
+/*
+ * Copyright 2011 mathieuancelin.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * under the License.
+ */
+
+package cx.ath.mancel01.dependencyshot.dynamic.config;
+
+import static java.lang.annotation.ElementType.TYPE;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ *
+ * @author mathieuancelin
+ */
+@Target({ TYPE })
+@Retention(RetentionPolicy.RUNTIME)
+public @interface Publish {
+ public Class[] contracts() default {};
+}
=======================================
--- /dev/null
+++
/trunk/dependency-shot-dynamic/src/main/java/cx/ath/mancel01/dependencyshot/dynamic/event/AbstractServiceEvent.java
Thu Apr 7 09:13:58 2011
@@ -0,0 +1,127 @@
+package cx.ath.mancel01.dependencyshot.dynamic.event;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceReference;
+
+/**
+ *
+ * @author Mathieu ANCELIN - SERLI (mathieu...@serli.com)
+ */
+public abstract class AbstractServiceEvent {
+
+ public static enum EventType {
+ SERVICE_ARRIVAL, SERVICE_DEPARTURE, SERVICE_CHANGED
+ }
+
+ private final ServiceReference ref;
+ private final BundleContext context;
+ private List<String> classesNames;
+ private List<Class<?>> classes;
+ private Map<Class, Boolean> assignable
+ = new HashMap<Class, Boolean>();
+
+ public AbstractServiceEvent(
+ ServiceReference ref, BundleContext context) {
+ this.ref = ref;
+ this.context = context;
+ }
+
+ public abstract EventType eventType();
+
+ public ServiceReference getRef() {
+ return ref;
+ }
+
+ public <T> TypedService<T> type(Class<T> type) {
+ if (isTyped(type)) {
+ return TypedService.create(type, context, ref);
+ } else {
+ throw new RuntimeException("the type " + type
+ + " isn't supported for the service. Supported types
are "
+ + getServiceClasses());
+ }
+ }
+
+ public Object getService() {
+ return context.getService(ref);
+ }
+
+ public boolean ungetService() {
+ return context.ungetService(ref);
+ }
+
+ public boolean isTyped(Class<?> type) {
+ boolean typed = false;
+ if (!assignable.containsKey(type)) {
+ for (Class clazz : getServiceClasses()) {
+ if (type.isAssignableFrom(clazz)) {
+ typed = true;
+ break;
+ }
+ }
+ assignable.put(type, typed);
+ }
+ return assignable.get(type);
+ }
+
+ public Bundle getRegisteringBundle() {
+ return ref.getBundle();
+ }
+
+ public List<String> getServiceClassNames() {
+ if (classesNames == null) {
+ classesNames = Arrays.asList((String[])
+ ref.getProperty(Constants.OBJECTCLASS));
+ }
+ return classesNames;
+ }
+
+ public List<Class<?>> getServiceClasses() {
+ if (classes == null) {
+ classes = new ArrayList<Class<?>>();
+ for (String className : getServiceClassNames()) {
+ try {
+ classes.add(getClass()
+ .getClassLoader().loadClass(className));
+ } catch (ClassNotFoundException ex) {
+ return null;
+ }
+ }
+ }
+ return classes;
+ }
+
+ public static class TypedService<T> {
+
+ private final BundleContext context;
+ private final ServiceReference ref;
+ private final Class<T> type;
+
+ TypedService(BundleContext context,
+ ServiceReference ref, Class<T> type) {
+ this.context = context;
+ this.ref = ref;
+ this.type = type;
+ }
+
+ static <T> TypedService<T> create(Class<T> type
+ , BundleContext context, ServiceReference ref) {
+ return new TypedService<T>(context, ref, type);
+ }
+
+ public T getService() {
+ return type.cast(context.getService(ref));
+ }
+
+ public boolean ungetService() {
+ return context.ungetService(ref);
+ }
+ }
+}
=======================================
--- /dev/null
+++
/trunk/dependency-shot-dynamic/src/main/java/cx/ath/mancel01/dependencyshot/dynamic/event/BundleContainerInitialized.java
Thu Apr 7 09:13:58 2011
@@ -0,0 +1,20 @@
+package cx.ath.mancel01.dependencyshot.dynamic.event;
+
+import org.osgi.framework.BundleContext;
+
+/**
+ *
+ * @author Mathieu ANCELIN - SERLI (mathieu...@serli.com)
+ */
+public class BundleContainerInitialized {
+
+ private BundleContext bundleContext;
+
+ public BundleContainerInitialized(final BundleContext context) {
+ this.bundleContext = context;
+ }
+
+ public BundleContext getBundleContext() {
+ return bundleContext;
+ }
+}
=======================================
--- /dev/null
+++
/trunk/dependency-shot-dynamic/src/main/java/cx/ath/mancel01/dependencyshot/dynamic/event/BundleContainerShutdown.java
Thu Apr 7 09:13:58 2011
@@ -0,0 +1,20 @@
+package cx.ath.mancel01.dependencyshot.dynamic.event;
+
+import org.osgi.framework.BundleContext;
+
+/**
+ *
+ * @author Mathieu ANCELIN - SERLI (mathieu...@serli.com)
+ */
+public class BundleContainerShutdown {
+
+ private BundleContext bundleContext;
+
+ public BundleContainerShutdown(final BundleContext context) {
+ this.bundleContext = context;
+ }
+
+ public BundleContext getBundleContext() {
+ return bundleContext;
+ }
+}
=======================================
--- /dev/null
+++
/trunk/dependency-shot-dynamic/src/main/java/cx/ath/mancel01/dependencyshot/dynamic/event/ServiceArrival.java
Thu Apr 7 09:13:58 2011
@@ -0,0 +1,21 @@
+package cx.ath.mancel01.dependencyshot.dynamic.event;
+
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
+
+/**
+ *
+ * @author mathieu
+ */
+public class ServiceArrival extends AbstractServiceEvent {
+
+ public ServiceArrival(
+ ServiceReference ref, BundleContext context) {
+ super(ref, context);
+ }
+
+ @Override
+ public EventType eventType() {
+ return EventType.SERVICE_ARRIVAL;
+ }
+}
=======================================
--- /dev/null
+++
/trunk/dependency-shot-dynamic/src/main/java/cx/ath/mancel01/dependencyshot/dynamic/event/ServiceChanged.java
Thu Apr 7 09:13:58 2011
@@ -0,0 +1,21 @@
+package cx.ath.mancel01.dependencyshot.dynamic.event;
+
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
+
+/**
+ *
+ * @author mathieu
+ */
+public class ServiceChanged extends AbstractServiceEvent {
+
+ public ServiceChanged(
+ ServiceReference ref, BundleContext context) {
+ super(ref, context);
+ }
+
+ @Override
+ public EventType eventType() {
+ return EventType.SERVICE_CHANGED;
+ }
+}
=======================================
--- /dev/null
+++
/trunk/dependency-shot-dynamic/src/main/java/cx/ath/mancel01/dependencyshot/dynamic/event/ServiceDeparture.java
Thu Apr 7 09:13:58 2011
@@ -0,0 +1,21 @@
+package cx.ath.mancel01.dependencyshot.dynamic.event;
+
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
+
+/**
+ *
+ * @author mathieu
+ */
+public class ServiceDeparture extends AbstractServiceEvent {
+
+ public ServiceDeparture(
+ ServiceReference ref, BundleContext context) {
+ super(ref, context);
+ }
+
+ @Override
+ public EventType eventType() {
+ return EventType.SERVICE_DEPARTURE;
+ }
+}
=======================================
--- /dev/null
+++
/trunk/dependency-shot-dynamic/src/main/java/cx/ath/mancel01/dependencyshot/dynamic/registry/OSGiServiceRegistryImpl.java
Thu Apr 7 09:13:58 2011
@@ -0,0 +1,121 @@
+/*
+ * Copyright 2010 mathieu.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * under the License.
+ */
+
+package cx.ath.mancel01.dependencyshot.dynamic.registry;
+
+import cx.ath.mancel01.dependencyshot.api.DSInjector;
+import cx.ath.mancel01.dependencyshot.api.event.EventManager;
+import cx.ath.mancel01.dependencyshot.event.EventManagerImpl;
+import java.util.ArrayList;
+import java.util.List;
+import javax.inject.Inject;
+import javax.inject.Singleton;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceEvent;
+import org.osgi.framework.ServiceListener;
+
+/**
+ *
+ * @author mathieu
+ */
+@Singleton
+public class OSGiServiceRegistryImpl implements ServiceRegistry,
ServiceListener {
+
+ @Inject
+ private DSInjector injector;
+
+ @Inject
+ private EventManager eventManager;
+
+ private BundleContext context;
+
+ public void setContext(BundleContext context) {
+ this.context = context;
+ context.addServiceListener(this);
+ }
+
+ @Override
+ public void addServiceListener(Class<?> listener) {
+ ((EventManagerImpl) eventManager).registerListener(listener);
+ }
+
+ @Override
+ public void removeServiceListener(Class<?> listener) {
+ throw new UnsupportedOperationException("Not supported yet.");
+ }
+
+ @Override
+ public <T> ServiceRegistration registerService(Class<T> clazz,
Class<?> service) {
+ return new ServiceRegistrationImpl(
+ context.registerService(clazz.getName(),
injector.getInstance(service), null));
+ }
+
+ @Override
+ public <T> ServiceRegistration registerService(Class<T> clazz, T
service) {
+ return new ServiceRegistrationImpl(
+ context.registerService(clazz.getName(), service, null));
+ }
+
+ @Override
+ public <T> ServiceRegistration registerService(Class<T>[] clazzes,
Class<?> service) {
+ List<String> names = new ArrayList<String>();
+ for (Class<T> clazz : clazzes) {
+ names.add(clazz.getName());
+ }
+ return new ServiceRegistrationImpl(
+ context.registerService(names.toArray(new String[] {}),
injector.getInstance(service), null));
+ }
+
+ @Override
+ public <T> ServiceRegistration registerService(Class<T>[] clazzes, T
service) {
+ List<String> names = new ArrayList<String>();
+ for (Class<T> clazz : clazzes) {
+ names.add(clazz.getName());
+ }
+ return new ServiceRegistrationImpl(
+ context.registerService(names.toArray(new String[] {}),
service, null));
+ }
+
+ @Override
+ public <T> T getService(Class<T> contract) {
+ return (T)
context.getService(context.getServiceReference(contract.getName()));
+ }
+
+ @Override
+ public <T> Iterable<T> getServices(Class<T> contract) {
+ throw new UnsupportedOperationException("Not supported yet.");
+ }
+
+ @Override
+ public void serviceChanged(ServiceEvent se) {
+ injector.fire(se);
+ }
+
+ private class ServiceRegistrationImpl<T> implements
ServiceRegistration {
+
+ final org.osgi.framework.ServiceRegistration reg;
+
+ public
ServiceRegistrationImpl(org.osgi.framework.ServiceRegistration reg) {
+ this.reg = reg;
+ }
+
+ @Override
+ public void unregister() {
+ reg.unregister();
+ }
+ }
+}
=======================================
--- /dev/null
+++
/trunk/dependency-shot-dynamic/src/main/java/cx/ath/mancel01/dependencyshot/dynamic/registry/ServiceRegistry.java
Thu Apr 7 09:13:58 2011
@@ -0,0 +1,51 @@
+/*
+ * Copyright 2011 mathieuancelin.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * under the License.
+ */
+
+package cx.ath.mancel01.dependencyshot.dynamic.registry;
+
+/**
+ *
+ * @author mathieuancelin
+ */
+public interface ServiceRegistry {
+
+ public void addServiceListener(Class<?> listener);
+
+ public void removeServiceListener(Class<?> listener);
+
+ public <T> ServiceRegistration registerService(Class<T> clazz,
Class<?> service);
+
+ public <T> ServiceRegistration registerService(Class<T> clazz, T
service);
+
+ public <T> ServiceRegistration registerService(Class<T>[] clazzes,
Class<?> service);
+
+ public <T> ServiceRegistration registerService(Class<T>[] clazzes, T
service);
+
+ public <T> T getService(Class<T> contract);
+
+ public <T> Iterable<T> getServices(Class<T> contract);
+
+// public <T> boolean ungetService(T service);
+//
+// public <T> boolean ungetServices(Collection<T> services);
+//
+// public Map<Class<?>, Collection<Class<?>>> getAvailableServices();
+
+ public static interface ServiceRegistration {
+ void unregister();
+ }
+}
=======================================
--- /dev/null
+++
/trunk/dependency-shot-dynamic/src/main/java/cx/ath/mancel01/dependencyshot/dynamic/registry/ServiceRegistryImpl.java
Thu Apr 7 09:13:58 2011
@@ -0,0 +1,184 @@
+/*
+ * Copyright 2010 mathieu.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * under the License.
+ */
+
+package cx.ath.mancel01.dependencyshot.dynamic.registry;
+
+import cx.ath.mancel01.dependencyshot.api.DSInjector;
+import cx.ath.mancel01.dependencyshot.api.event.EventManager;
+import cx.ath.mancel01.dependencyshot.dynamic.Dynamic;
+import cx.ath.mancel01.dependencyshot.event.EventManagerImpl;
+import cx.ath.mancel01.dependencyshot.exceptions.DSIllegalStateException;
+import cx.ath.mancel01.dependencyshot.injection.InjectorImpl;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import javax.inject.Inject;
+import javax.inject.Singleton;
+
+/**
+ *
+ * @author mathieu
+ */
+@Singleton
+public class ServiceRegistryImpl implements ServiceRegistry {
+
+ @Inject
+ private DSInjector injector;
+
+ @Inject
+ private EventManager eventManager;
+
+ private ConcurrentHashMap<Class<?>, Collection<Class<?>>> services =
+ new ConcurrentHashMap<Class<?>, Collection<Class<?>>>();
+
+ @Override
+ public String toString() {
+ StringBuilder builder = new StringBuilder();
+ for (Class<?> clazz : services.keySet()) {
+ builder.append(clazz.getName());
+ ArrayList<Class<?>> classes = (ArrayList<Class<?>>)
services.get(clazz);
+ for (Class<?> service : classes) {
+ builder.append("\n => ");
+ builder.append(service.getName());
+ }
+ builder.append("\n\n");
+ }
+ return builder.toString();
+ }
+
+ @Override
+ public void addServiceListener(Class<?> listener) {
+ ((EventManagerImpl) eventManager).registerListener(listener);
+ }
+
+ @Override
+ public void removeServiceListener(Class<?> listener) {
+ throw new UnsupportedOperationException("Not supported yet.");
+ }
+
+ @Override
+ public <T> ServiceRegistration registerService(Class<T> clazz,
Class<?> service) {
+ if(!service.isAnnotationPresent(Dynamic.class)) {
+ throw new DSIllegalStateException("You can't register non
dynamic implementation for a service");
+ }
+ services.putIfAbsent(clazz, new ArrayList<Class<?>>());
+ ArrayList<Class<?>> classes = (ArrayList<Class<?>>)
services.get(clazz);
+ if (!classes.contains(service)) {
+ synchronized(classes) {
+ classes.add(service);
+ }
+ }
+ List<Class<?>> itfs = new ArrayList<Class<?>>();
+ itfs.add(clazz);
+ return new ServiceRegistrationImpl(itfs, service);
+ }
+
+ @Override
+ public <T> ServiceRegistration registerService(Class<T> clazz, T
service) {
+ throw new UnsupportedOperationException("Not supported yet.");
+ }
+
+ @Override
+ public <T> ServiceRegistration registerService(Class<T>[] clazzes,
Class<?> service) {
+ for (Class<T> clazz : clazzes) {
+ registerService(clazz, service);
+ }
+ return new ServiceRegistrationImpl(Arrays.asList(clazzes),
service);
+ }
+
+ @Override
+ public <T> ServiceRegistration registerService(Class<T>[] clazzes, T
service) {
+ throw new UnsupportedOperationException("Not supported yet.");
+ }
+
+ @Override
+ public <T> T getService(Class<T> contract) {
+ return (T) ((InjectorImpl)injector)
+ .getUnscopedInstance(((ArrayList<Class<?>>)
services.get(contract)).get(0));
+ }
+
+ @Override
+ public <T> Iterable<T> getServices(Class<T> contract) {
+ Collection<T> objects = new ArrayList<T>();
+ for (Class<?> clazz : services.get(contract)) {
+ objects.add((T)
((InjectorImpl)injector).getUnscopedInstance(clazz));
+ }
+ return objects;
+ }
+
+ public Map<Class<?>, Collection<Class<?>>> getAvailableServices() {
+ return services;
+ }
+
+ public void swap(Class<?> to) {
+ if(!to.isAnnotationPresent(Dynamic.class)) {
+ throw new DSIllegalStateException("You can't register non
dynamic implementation for a service");
+ }
+ for (Class from : to.getInterfaces()) {
+ ArrayList<Class<?>> classes = (ArrayList<Class<?>>)
services.get(from);
+ if (classes.contains(to)) {
+ int index = classes.indexOf(to);
+ Class<?> old = classes.set(0, to);
+ classes.set(index, old);
+ } else {
+ registerServiceAndSwap(from, to);
+ }
+ }
+ }
+
+ public void registerServiceAndSwap(Class<?> from, Class<?> to) {
+ if(!to.isAnnotationPresent(Dynamic.class)) {
+ throw new DSIllegalStateException("You can't register non
dynamic implementation for a service");
+ }
+ services.putIfAbsent(from, new ArrayList<Class<?>>());
+ ArrayList<Class<?>> classes = (ArrayList<Class<?>>)
services.get(from);
+ if (!classes.contains(to)) {
+ synchronized(classes) {
+ classes.add(0, to);
+ }
+ }
+ }
+
+ public Class<?> getContract(Class<?> from) {
+ return ((ArrayList<Class<?>>) services.get(from)).get(0);
+ }
+
+ public Collection<Class<?>> multipleContractsLookup(Class<?> from) {
+ return services.get(from);
+ }
+
+ private class ServiceRegistrationImpl<T> implements
ServiceRegistration {
+
+ final List<Class<T>> classes;
+ final Class<? extends T> to;
+
+ public ServiceRegistrationImpl(List<Class<T>> classes, Class<?
extends T> to) {
+ this.classes = classes;
+ this.to = to;
+ }
+
+ @Override
+ public void unregister() {
+ for (Class from : classes) {
+ services.get(from).remove(to);
+ }
+ }
+ }
+}
=======================================
--- /dev/null
+++
/trunk/dependency-shot-dynamic/src/main/java/cx/ath/mancel01/dependencyshot/dynamic/registry/ServiceRegistryProvider.java
Thu Apr 7 09:13:58 2011
@@ -0,0 +1,56 @@
+/*
+ * Copyright 2011 mathieuancelin.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * under the License.
+ */
+
+package cx.ath.mancel01.dependencyshot.dynamic.registry;
+
+import javax.inject.Inject;
+import javax.inject.Provider;
+import javax.inject.Singleton;
+
+/**
+ *
+ * @author mathieuancelin
+ */
+public class ServiceRegistryProvider implements Provider<ServiceRegistry> {
+
+ @Inject OSGiEnvHolder holder;
+ @Inject ServiceRegistryImpl internalService;
+ @Inject OSGiServiceRegistryImpl osgiService;
+
+ @Override
+ public ServiceRegistry get() {
+ if (holder.isOsgi()) {
+ return osgiService;
+ } else {
+ return internalService;
+ }
+ }
+
+ @Singleton
+ public static class OSGiEnvHolder {
+
+ private boolean osgi = false;
+
+ public void setOsgi(boolean osgi) {
+ this.osgi = osgi;
+ }
+
+ public boolean isOsgi() {
+ return osgi;
+ }
+ }
+}
=======================================
--- /dev/null
+++
/trunk/dependency-shot-dynamic/src/main/java/cx/ath/mancel01/dependencyshot/dynamic/scope/DynamicImplementation.java
Thu Apr 7 09:13:58 2011
@@ -0,0 +1,28 @@
+/*
+ * Copyright 2010 mathieu.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * under the License.
+ */
+
+package cx.ath.mancel01.dependencyshot.dynamic.scope;
+
+import cx.ath.mancel01.dependencyshot.dynamic.Dynamic;
+
+/**
+ * Dummy class to provide fake dynamic binding.
+ *
+ * @author mathieu
+ */
+@Dynamic
+public class DynamicImplementation {}
=======================================
--- /dev/null
+++
/trunk/dependency-shot-dynamic/src/main/java/cx/ath/mancel01/dependencyshot/dynamic/scope/DynamicProxy.java
Thu Apr 7 09:13:58 2011
@@ -0,0 +1,81 @@
+/*
+ * Copyright 2010 mathieuancelin.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * under the License.
+ */
+
+package cx.ath.mancel01.dependencyshot.dynamic.scope;
+
+import cx.ath.mancel01.dependencyshot.dynamic.registry.ServiceRegistry;
+import cx.ath.mancel01.dependencyshot.api.InjectionPoint;
+import cx.ath.mancel01.dependencyshot.injection.InjectorImpl;
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+import javassist.util.proxy.MethodFilter;
+import javassist.util.proxy.MethodHandler;
+
+/**
+ *
+ * @author Mathieu ANCELIN
+ */
+public class DynamicProxy<T> implements MethodFilter, MethodHandler,
InvocationHandler {
+
+ private final Class<T> from;
+
+ private final InjectionPoint point;
+
+ private final InjectorImpl injector;
+
+ private final ServiceRegistry registry;
+
+ private Object actualService;
+
+ public DynamicProxy(
+ Class<T> from, InjectionPoint point,
+ InjectorImpl injector, ServiceRegistry registry) {
+ this.from = from;
+ this.injector= injector;
+ this.registry = registry;
+ this.point = point;
+ }
+
+ public Class<T> getFrom() {
+ return from;
+ }
+
+ public InjectionPoint getPoint() {
+ return point;
+ }
+
+ public InjectorImpl getInjector() {
+ return injector;
+ }
+
+ @Override
+ public boolean isHandled(Method method) {
+ return true;
+ }
+
+ @Override
+ public Object invoke(Object self, Method method, Method proceed,
Object[] args) throws Throwable {
+ actualService = registry.getService(from);
+ return method.invoke(actualService, args);
+ }
+
+ @Override
+ public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
+ actualService = registry.getService(from);
+ return method.invoke(actualService, args);
+ }
+}
=======================================
--- /dev/null
+++
/trunk/dependency-shot-dynamic/src/main/java/cx/ath/mancel01/dependencyshot/dynamic/scope/DynamicScope.java
Thu Apr 7 09:13:58 2011
@@ -0,0 +1,63 @@
+/*
+ * Copyright 2010 mathieuancelin.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * under the License.
+ */
+
+package cx.ath.mancel01.dependencyshot.dynamic.scope;
+
+import cx.ath.mancel01.dependencyshot.dynamic.registry.ServiceRegistry;
+import cx.ath.mancel01.dependencyshot.api.InjectionPoint;
+import cx.ath.mancel01.dependencyshot.dynamic.Dynamic;
+import cx.ath.mancel01.dependencyshot.injection.InjectorImpl;
+import cx.ath.mancel01.dependencyshot.spi.CustomScopeHandler;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Proxy;
+
+/**
+ *
+ * @author Mathieu ANCELIN
+ */
+public class DynamicScope extends CustomScopeHandler {
+
+ @Override
+ public Class<? extends Annotation> getScope() {
+ return Dynamic.class;
+ }
+
+ @Override
+ public <T> T getScopedInstance(Class<T> interf, Class<? extends T>
clazz,
+ InjectionPoint point, InjectorImpl injector) {
+
+ DynamicProxy proxy = new DynamicProxy(interf, point,
+ injector, injector.getInstance(ServiceRegistry.class));
+
+ return (T) Proxy.newProxyInstance(
+ getClass().getClassLoader(), new Class[] {interf}, proxy);
+ //return (T) ProxyHelper.createProxy(proxy);
+ }
+
+ @Override
+ public void reset() {}
+
+ @Override
+ public boolean isDynamic() {
+ return false;
+ }
+
+ @Override
+ public boolean isBeanValid(Class from, Class to) {
+ return true;
+ }
+}
=======================================
--- /dev/null
+++
/trunk/dependency-shot-dynamic/src/main/java/cx/ath/mancel01/dependencyshot/dynamic/scope/ProxyHelper.java
Thu Apr 7 09:13:58 2011
@@ -0,0 +1,49 @@
+/*
+ * Copyright 2010 mathieuancelin.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * under the License.
+ */
+
+package cx.ath.mancel01.dependencyshot.dynamic.scope;
+
+import javassist.util.proxy.ProxyFactory;
+import javassist.util.proxy.ProxyObject;
+
+/**
+ * Helper for proxy creation.
+ *
+ * @author Mathieu ANCELIN
+ */
+public class ProxyHelper {
+
+ public static <T> T createProxy(DynamicProxy handler) {
+ ProxyFactory fact = new ProxyFactory();
+ Class<T> from = handler.getFrom();
+ if (from.isInterface()) {
+ fact.setInterfaces(new Class[] {from});
+ } else {
+ fact.setSuperclass(from);
+ }
+ fact.setFilter(handler);
+ Class newBeanClass = fact.createClass();
+ T scopedObject = null;
+ try {
+ scopedObject = (T)
newBeanClass.cast(newBeanClass.newInstance());
+ } catch (Exception ex) {
+ throw new IllegalStateException("Unable to create proxy for
object " + from.getSimpleName(), ex);
+ }
+ ((ProxyObject) scopedObject).setHandler(handler);
+ return scopedObject;
+ }
+}
=======================================
---
/trunk/dependency-shot-dynamic/src/main/java/cx/ath/mancel01/dependencyshot/dynamic/DynamicBinder.java
Wed Nov 17 11:31:14 2010
+++ /dev/null
@@ -1,98 +0,0 @@
-/*
- * Copyright 2010 mathieu.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- * under the License.
- */
-
-package cx.ath.mancel01.dependencyshot.dynamic;
-
-import cx.ath.mancel01.dependencyshot.api.DSBinder;
-import cx.ath.mancel01.dependencyshot.exceptions.DSIllegalStateException;
-import cx.ath.mancel01.dependencyshot.graph.Binder;
-import cx.ath.mancel01.dependencyshot.graph.BinderAccessor;
-import cx.ath.mancel01.dependencyshot.graph.Binding;
-import cx.ath.mancel01.dependencyshot.injection.InjectorImpl;
-import java.lang.reflect.Field;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.List;
-import java.util.logging.Level;
-import java.util.logging.Logger;
-
-/**
- *
- * @author mathieu
- */
-public abstract class DynamicBinder extends Binder {
-
- private static final Logger logger =
Logger.getLogger(DynamicBinder.class.getName());
-
- private List<DSBinder> delegateBinders = new ArrayList<DSBinder>();
-
- public DynamicBinder() {
- }
-
- public DynamicBinder(DSBinder... binders) {
- delegateBinders.addAll(Arrays.asList(binders));
- }
-
- @Override
- public final void configureBindings() {
- for (DSBinder binder: delegateBinders) {
- BinderAccessor.setInjector((Binder) binder, (InjectorImpl)
injector());
- binder.configureBindings();
- BinderAccessor.configureLastBinding((Binder) binder);
- }
- configure();
- }
-
- public abstract void configure();
-
- public void bindDynamically(Class<?> from) {
- try {
- bind(from);
- Field fTo = Binder.class.getDeclaredField("to");
- fTo.setAccessible(true);
- fTo.set(this, DynamicImplementation.class);
- fTo.setAccessible(false);
- } catch (Exception ex) {
- logger.log(Level.SEVERE, "Unable to bind dynamically the
class " + from.getName(), ex);
- }
- validate(from);
- }
-
- private void validate(Class<?> dynamic) {
- ArrayList<Class<?>> errors = new ArrayList<Class<?>>();
- Collection<Binding<?>> registered = this.getBindings().keySet();
- for (Binding binding : registered) {
- if (binding.getFrom() != null && binding.getTo() != null
- && dynamic.equals(binding.getFrom())
-
&& !binding.getTo().equals(DynamicImplementation.class)) {
- errors.add(binding.getFrom());
- }
- }
- if (!errors.isEmpty()) {
- StringBuilder builder = new StringBuilder();
- builder.append("The following classes are both declared for
dynamic injection and static injection.");
- builder.append("\nIt can't append !\n");
- for (Class<?> clazz : errors) {
- builder.append(" ");
- builder.append(clazz.getName());
- builder.append("\n");
- }
- throw new DSIllegalStateException(builder.toString());
- }
- }
-}
=======================================
---
/trunk/dependency-shot-dynamic/src/main/java/cx/ath/mancel01/dependencyshot/dynamic/DynamicImplementation.java
Thu Nov 4 10:23:53 2010
+++ /dev/null
@@ -1,26 +0,0 @@
-/*
- * Copyright 2010 mathieu.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- * under the License.
- */
-
-package cx.ath.mancel01.dependencyshot.dynamic;
-
-/**
- * Dummy class to provide fake dynamic binding.
- *
- * @author mathieu
- */
-@Dynamic
-public class DynamicImplementation {}
=======================================
---
/trunk/dependency-shot-dynamic/src/main/java/cx/ath/mancel01/dependencyshot/dynamic/DynamicProvider.java
Thu Apr 7 09:13:25 2011
+++ /dev/null
@@ -1,54 +0,0 @@
-/*
- * Copyright 2009-2010 Mathieu ANCELIN.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- * under the License.
- */
-
-package cx.ath.mancel01.dependencyshot.dynamic;
-
-import cx.ath.mancel01.dependencyshot.graph.Binding;
-import cx.ath.mancel01.dependencyshot.graph.BindingBuilder;
-import cx.ath.mancel01.dependencyshot.injection.InjectorImpl;
-import cx.ath.mancel01.dependencyshot.spi.BindingsProvider;
-import java.util.ArrayList;
-import java.util.Collection;
-
-/**
- *
- * @author Mathieu ANCELIN
- */
-public class DynamicProvider extends BindingsProvider {
-
- @Override
- public Collection<Binding> getProvidedBindings(InjectorImpl injector) {
-
- ArrayList<Binding> bindings = new ArrayList<Binding>();
-
- bindings.add(BindingBuilder
- .prepareBindingThat()
- .bind(ServiceRegistry.class)
- .annotatedWith(Internal.class)
- .to(ServiceRegistryImpl.class)
- .build());
- bindings.add(BindingBuilder
- .prepareBindingThat()
- .bind(ServiceRegistry.class)
- .annotatedWith(OSGi.class)
- .to(OSGiServiceRegistryImpl.class)
- .build());
-
- return bindings;
- }
-
-}
=======================================
---
/trunk/dependency-shot-dynamic/src/main/java/cx/ath/mancel01/dependencyshot/dynamic/DynamicProxy.java
Thu Apr 7 09:13:25 2011
+++ /dev/null
@@ -1,80 +0,0 @@
-/*
- * Copyright 2010 mathieuancelin.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- * under the License.
- */
-
-package cx.ath.mancel01.dependencyshot.dynamic;
-
-import cx.ath.mancel01.dependencyshot.api.InjectionPoint;
-import cx.ath.mancel01.dependencyshot.injection.InjectorImpl;
-import java.lang.reflect.InvocationHandler;
-import java.lang.reflect.Method;
-import javassist.util.proxy.MethodFilter;
-import javassist.util.proxy.MethodHandler;
-
-/**
- *
- * @author Mathieu ANCELIN
- */
-public class DynamicProxy<T> implements MethodFilter, MethodHandler,
InvocationHandler {
-
- private final Class<T> from;
-
- private final InjectionPoint point;
-
- private final InjectorImpl injector;
-
- private final ServiceRegistryImpl registry;
-
- private Object actualService;
-
- public DynamicProxy(
- Class<T> from, InjectionPoint point,
- InjectorImpl injector, ServiceRegistryImpl registry) {
- this.from = from;
- this.injector= injector;
- this.registry = registry;
- this.point = point;
- }
-
- public Class<T> getFrom() {
- return from;
- }
-
- public InjectionPoint getPoint() {
- return point;
- }
-
- public InjectorImpl getInjector() {
- return injector;
- }
-
- @Override
- public boolean isHandled(Method method) {
- return true;
- }
-
- @Override
- public Object invoke(Object self, Method method, Method proceed,
Object[] args) throws Throwable {
- actualService = registry.getService(from);
- return method.invoke(actualService, args);
- }
-
- @Override
- public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
- actualService = registry.getService(from);
- return method.invoke(actualService, args);
- }
-}
=======================================
---
/trunk/dependency-shot-dynamic/src/main/java/cx/ath/mancel01/dependencyshot/dynamic/DynamicScope.java
Thu Apr 7 09:13:25 2011
+++ /dev/null
@@ -1,58 +0,0 @@
-/*
- * Copyright 2010 mathieuancelin.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- * under the License.
- */
-
-package cx.ath.mancel01.dependencyshot.dynamic;
-
-import cx.ath.mancel01.dependencyshot.api.InjectionPoint;
-import cx.ath.mancel01.dependencyshot.injection.InjectorImpl;
-import cx.ath.mancel01.dependencyshot.spi.CustomScopeHandler;
-import java.lang.annotation.Annotation;
-import java.lang.reflect.Proxy;
-
-/**
- *
- * @author Mathieu ANCELIN
- */
-public class DynamicScope extends CustomScopeHandler {
-
- @Override
- public Class<? extends Annotation> getScope() {
- return Dynamic.class;
- }
-
- @Override
- public <T> T getScopedInstance(Class<T> interf, Class<? extends T>
clazz,
- InjectionPoint point, InjectorImpl injector) {
- DynamicProxy proxy = new DynamicProxy(interf, point,
- injector, injector.getInstance(ServiceRegistryImpl.class));
- return (T) Proxy.newProxyInstance(getClass().getClassLoader(), new
Class[] {interf}, proxy);
- //return (T) ProxyHelper.createProxy(proxy);
- }
-
- @Override
- public void reset() {}
-
- @Override
- public boolean isDynamic() {
- return false;
- }
-
- @Override
- public boolean isBeanValid(Class from, Class to) {
- return true;
- }
-}
=======================================
---
/trunk/dependency-shot-dynamic/src/main/java/cx/ath/mancel01/dependencyshot/dynamic/Internal.java
Thu Apr 7 09:13:25 2011
+++ /dev/null
@@ -1,32 +0,0 @@
-/*
- * Copyright 2009-2010 Mathieu ANCELIN.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- * under the License.
- */
-
-package cx.ath.mancel01.dependencyshot.dynamic;
-
-import java.lang.annotation.Retention;
-import java.lang.annotation.RetentionPolicy;
-import javax.inject.Qualifier;
-
-/**
- *
- * @author Mathieu ANCELIN
- */
-@Retention(RetentionPolicy.RUNTIME)
-@Qualifier
-public @interface Internal {
-
-}
=======================================
---
/trunk/dependency-shot-dynamic/src/main/java/cx/ath/mancel01/dependencyshot/dynamic/OSGiConfigurator.java
Thu Apr 7 09:13:25 2011
+++ /dev/null
@@ -1,73 +0,0 @@
-/*
- * Copyright 2009-2010 Mathieu ANCELIN.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- * under the License.
- */
-package cx.ath.mancel01.dependencyshot.dynamic;
-
-import cx.ath.mancel01.dependencyshot.DependencyShot;
-import cx.ath.mancel01.dependencyshot.api.Stage;
-import cx.ath.mancel01.dependencyshot.graph.Binder;
-import cx.ath.mancel01.dependencyshot.injection.InjectorImpl;
-import cx.ath.mancel01.dependencyshot.spi.ConfigurationHandler;
-import javax.inject.Inject;
-import org.osgi.framework.BundleContext;
-
-/**
- *
- * @author Mathieu ANCELIN
- */
-public class OSGiConfigurator extends ConfigurationHandler {
-
- private BundleContext context;
- private Binder binder;
-
- @Inject @OSGi
- private ServiceRegistry registry;
-
- @Override
- public InjectorImpl getInjector(Stage stage) {
- InjectorImpl injector = null;
- if (context == null) {
- throw new IllegalStateException("You must set the current
bundle context.");
- }
- if (binder == null) {
- // TODO :
- injector = DependencyShot.getInjector();
- } else {
- injector = DependencyShot.getInjector(binder);
- }
- injector.injectInstance(this);
- if (registry == null) {
- throw new IllegalStateException("Injection for bundle context
failed.");
- }
- ((OSGiServiceRegistryImpl) registry).setContext(context);
- return injector;
- }
-
- @Override
- public boolean isAutoEnabled() {
- return false;
- }
-
- public OSGiConfigurator withBinder(Binder binder) {
- this.binder = binder;
- return this;
- }
-
- public OSGiConfigurator context(BundleContext context) {
- this.context = context;
- return this;
- }
-}
=======================================
---
/trunk/dependency-shot-dynamic/src/main/java/cx/ath/mancel01/dependencyshot/dynamic/ProxyHelper.java
Thu Nov 4 10:23:53 2010
+++ /dev/null
@@ -1,49 +0,0 @@
-/*
- * Copyright 2010 mathieuancelin.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- * under the License.
- */
-
-package cx.ath.mancel01.dependencyshot.dynamic;
-
-import javassist.util.proxy.ProxyFactory;
-import javassist.util.proxy.ProxyObject;
-
-/**
- * Helper for proxy creation.
- *
- * @author Mathieu ANCELIN
- */
-public class ProxyHelper {
-
- public static <T> T createProxy(DynamicProxy handler) {
- ProxyFactory fact = new ProxyFactory();
- Class<T> from = handler.getFrom();
- if (from.isInterface()) {
- fact.setInterfaces(new Class[] {from});
- } else {
- fact.setSuperclass(from);
- }
- fact.setFilter(handler);
- Class newBeanClass = fact.createClass();
- T scopedObject = null;
- try {
- scopedObject = (T)
newBeanClass.cast(newBeanClass.newInstance());
- } catch (Exception ex) {
- throw new IllegalStateException("Unable to create proxy for
object " + from.getSimpleName(), ex);
- }
- ((ProxyObject) scopedObject).setHandler(handler);
- return scopedObject;
- }
-}
=======================================
---
/trunk/dependency-shot-dynamic/src/main/java/cx/ath/mancel01/dependencyshot/dynamic/ServiceRegistry.java
Thu Apr 7 09:13:25 2011
+++ /dev/null
@@ -1,51 +0,0 @@
-/*
- * Copyright 2011 mathieuancelin.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- * under the License.
- */
-
-package cx.ath.mancel01.dependencyshot.dynamic;
-
-/**
- *
- * @author mathieuancelin
- */
-public interface ServiceRegistry {
-
- public void addServiceListener(Class<?> listener);
-
- public void removeServiceListener(Class<?> listener);
-
- public <T> ServiceRegistration registerService(Class<T> clazz,
Class<? extends T> service);
-
- public <T> ServiceRegistration registerService(Class<T> clazz, T
service);
-
- public <T> ServiceRegistration registerService(Class<T>[] clazzes,
Class<? extends T> service);
-
- public <T> ServiceRegistration registerService(Class<T>[] clazzes, T
service);
-
- public <T> T getService(Class<T> contract);
-
- public <T> Iterable<T> getServices(Class<T> contract);
-
-// public <T> boolean ungetService(T service);
-//
-// public <T> boolean ungetServices(Collection<T> services);
-//
-// public Map<Class<?>, Collection<Class<?>>> getAvailableServices();
-
- public static interface ServiceRegistration {
- void unregister();
- }
-}
=======================================
---
/trunk/dependency-shot-dynamic/src/main/java/cx/ath/mancel01/dependencyshot/dynamic/ServiceRegistryImpl.java
Thu Apr 7 09:13:25 2011
+++ /dev/null
@@ -1,183 +0,0 @@
-/*
- * Copyright 2010 mathieu.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- * under the License.
- */
-
-package cx.ath.mancel01.dependencyshot.dynamic;
-
-import cx.ath.mancel01.dependencyshot.api.DSInjector;
-import cx.ath.mancel01.dependencyshot.api.event.EventManager;
-import cx.ath.mancel01.dependencyshot.event.EventManagerImpl;
-import cx.ath.mancel01.dependencyshot.exceptions.DSIllegalStateException;
-import cx.ath.mancel01.dependencyshot.injection.InjectorImpl;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.List;
-import java.util.Map;
-import java.util.concurrent.ConcurrentHashMap;
-import javax.inject.Inject;
-import javax.inject.Singleton;
-
-/**
- *
- * @author mathieu
- */
-@Singleton
-public class ServiceRegistryImpl implements ServiceRegistry {
-
- @Inject
- private DSInjector injector;
-
- @Inject
- private EventManager eventManager;
-
- private ConcurrentHashMap<Class<?>, Collection<Class<?>>> services =
- new ConcurrentHashMap<Class<?>, Collection<Class<?>>>();
-
- @Override
- public String toString() {
- StringBuilder builder = new StringBuilder();
- for (Class<?> clazz : services.keySet()) {
- builder.append(clazz.getName());
- ArrayList<Class<?>> classes = (ArrayList<Class<?>>)
services.get(clazz);
- for (Class<?> service : classes) {
- builder.append("\n => ");
- builder.append(service.getName());
- }
- builder.append("\n\n");
- }
- return builder.toString();
- }
-
- @Override
- public void addServiceListener(Class<?> listener) {
- ((EventManagerImpl) eventManager).registerListener(listener);
- }
-
- @Override
- public void removeServiceListener(Class<?> listener) {
- throw new UnsupportedOperationException("Not supported yet.");
- }
-
- @Override
- public <T> ServiceRegistration registerService(Class<T> clazz, Class<?
extends T> service) {
- if(!service.isAnnotationPresent(Dynamic.class)) {
- throw new DSIllegalStateException("You can't register non
dynamic implementation for a service");
- }
- services.putIfAbsent(clazz, new ArrayList<Class<?>>());
- ArrayList<Class<?>> classes = (ArrayList<Class<?>>)
services.get(clazz);
- if (!classes.contains(service)) {
- synchronized(classes) {
- classes.add(service);
- }
- }
- List<Class<?>> itfs = new ArrayList<Class<?>>();
- itfs.add(clazz);
- return new ServiceRegistrationImpl(itfs, service);
- }
-
- @Override
- public <T> ServiceRegistration registerService(Class<T> clazz, T
service) {
- throw new UnsupportedOperationException("Not supported yet.");
- }
-
- @Override
- public <T> ServiceRegistration registerService(Class<T>[] clazzes,
Class<? extends T> service) {
- for (Class<T> clazz : clazzes) {
- registerService(clazz, service);
- }
- return new ServiceRegistrationImpl(Arrays.asList(clazzes),
service);
- }
-
- @Override
- public <T> ServiceRegistration registerService(Class<T>[] clazzes, T
service) {
- throw new UnsupportedOperationException("Not supported yet.");
- }
-
- @Override
- public <T> T getService(Class<T> contract) {
- return (T) ((InjectorImpl)injector)
- .getUnscopedInstance(((ArrayList<Class<?>>)
services.get(contract)).get(0));
- }
-
- @Override
- public <T> Iterable<T> getServices(Class<T> contract) {
- Collection<T> objects = new ArrayList<T>();
- for (Class<?> clazz : services.get(contract)) {
- objects.add((T)
((InjectorImpl)injector).getUnscopedInstance(clazz));
- }
- return objects;
- }
-
- public Map<Class<?>, Collection<Class<?>>> getAvailableServices() {
- return services;
- }
-
- public void swap(Class<?> to) {
- if(!to.isAnnotationPresent(Dynamic.class)) {
- throw new DSIllegalStateException("You can't register non
dynamic implementation for a service");
- }
- for (Class from : to.getInterfaces()) {
- ArrayList<Class<?>> classes = (ArrayList<Class<?>>)
services.get(from);
- if (classes.contains(to)) {
- int index = classes.indexOf(to);
- Class<?> old = classes.set(0, to);
- classes.set(index, old);
- } else {
- registerServiceAndSwap(from, to);
- }
- }
- }
-
- public void registerServiceAndSwap(Class<?> from, Class<?> to) {
- if(!to.isAnnotationPresent(Dynamic.class)) {
- throw new DSIllegalStateException("You can't register non
dynamic implementation for a service");
- }
- services.putIfAbsent(from, new ArrayList<Class<?>>());
- ArrayList<Class<?>> classes = (ArrayList<Class<?>>)
services.get(from);
- if (!classes.contains(to)) {
- synchronized(classes) {
- classes.add(0, to);
- }
- }
- }
-
- public Class<?> getContract(Class<?> from) {
- return ((ArrayList<Class<?>>) services.get(from)).get(0);
- }
-
- public Collection<Class<?>> multipleContractsLookup(Class<?> from) {
- return services.get(from);
- }
-
- private class ServiceRegistrationImpl<T> implements
ServiceRegistration {
-
- final List<Class<T>> classes;
- final Class<? extends T> to;
-
- public ServiceRegistrationImpl(List<Class<T>> classes, Class<?
extends T> to) {
- this.classes = classes;
- this.to = to;
- }
-
- @Override
- public void unregister() {
- for (Class from : classes) {
- services.get(from).remove(to);
- }
- }
- }
-}
=======================================
---
/trunk/dependency-shot-dynamic/src/main/resources/META-INF/services/cx.ath.mancel01.dependencyshot.spi.BindingsProvider
Thu Apr 7 09:13:25 2011
+++
/trunk/dependency-shot-dynamic/src/main/resources/META-INF/services/cx.ath.mancel01.dependencyshot.spi.BindingsProvider
Thu Apr 7 09:13:58 2011
@@ -1,1 +1,1 @@
-cx.ath.mancel01.dependencyshot.dynamic.DynamicProvider
+cx.ath.mancel01.dependencyshot.dynamic.config.DynamicProvider
=======================================
---
/trunk/dependency-shot-dynamic/src/main/resources/META-INF/services/cx.ath.mancel01.dependencyshot.spi.ConfigurationHandler
Thu Apr 7 09:13:25 2011
+++
/trunk/dependency-shot-dynamic/src/main/resources/META-INF/services/cx.ath.mancel01.dependencyshot.spi.ConfigurationHandler
Thu Apr 7 09:13:58 2011
@@ -1,1 +1,1 @@
-cx.ath.mancel01.dependencyshot.dynamic.OSGiConfigurator
+cx.ath.mancel01.dependencyshot.dynamic.config.OSGiConfigurator
=======================================
---
/trunk/dependency-shot-dynamic/src/main/resources/META-INF/services/cx.ath.mancel01.dependencyshot.spi.CustomScopeHandler
Thu Apr 7 09:13:25 2011
+++
/trunk/dependency-shot-dynamic/src/main/resources/META-INF/services/cx.ath.mancel01.dependencyshot.spi.CustomScopeHandler
Thu Apr 7 09:13:58 2011
@@ -1,1 +1,1 @@
-cx.ath.mancel01.dependencyshot.dynamic.DynamicScope
+cx.ath.mancel01.dependencyshot.dynamic.scope.DynamicScope
=======================================
---
/trunk/dependency-shot-dynamic/src/test/java/cx/ath/mancel01/dependencyshot/dynamic/DynamicTest.java
Thu Apr 7 09:13:25 2011
+++
/trunk/dependency-shot-dynamic/src/test/java/cx/ath/mancel01/dependencyshot/dynamic/DynamicTest.java
Thu Apr 7 09:13:58 2011
@@ -17,6 +17,9 @@
package cx.ath.mancel01.dependencyshot.dynamic;
+import cx.ath.mancel01.dependencyshot.dynamic.registry.ServiceRegistryImpl;
+import cx.ath.mancel01.dependencyshot.dynamic.registry.ServiceRegistry;
+import cx.ath.mancel01.dependencyshot.dynamic.config.DynamicBinder;
import cx.ath.mancel01.dependencyshot.DependencyShot;
import cx.ath.mancel01.dependencyshot.api.DSBinder;
import cx.ath.mancel01.dependencyshot.api.DSInjector;
=======================================
---
/trunk/dependency-shot-dynamic/src/test/resources/META-INF/services/cx.ath.mancel01.dependencyshot.spi.CustomScopeHandler
Thu Nov 4 10:23:53 2010
+++
/trunk/dependency-shot-dynamic/src/test/resources/META-INF/services/cx.ath.mancel01.dependencyshot.spi.CustomScopeHandler
Thu Apr 7 09:13:58 2011
@@ -1,1 +1,1 @@
-cx.ath.mancel01.dependencyshot.dynamic.DynamicScope
+cx.ath.mancel01.dependencyshot.dynamic.scope.DynamicScope