You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by jb...@apache.org on 2007/03/10 01:10:22 UTC
svn commit: r516617 - in /incubator/tuscany/java/sca/kernel/core/src:
main/java/org/apache/tuscany/core/component/
main/java/org/apache/tuscany/core/component/scope/
main/java/org/apache/tuscany/core/implementation/system/component/
main/java/org/apach...
Author: jboynes
Date: Fri Mar 9 16:10:21 2007
New Revision: 516617
URL: http://svn.apache.org/viewvc?view=rev&rev=516617
Log:
add a generic instance factory implmentation that uses reflection
this will let us get the physical component working without requiring bytecode generation
Added:
incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/component/ReflectiveInstanceFactory.java (with props)
incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/component/scope/ReflectiveInstanceWrapper.java (with props)
incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/component/SystemComponent.java (with props)
incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/model/SystemPhysicalComponentDefinition.java (with props)
incubator/tuscany/java/sca/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/InstanceWrapperBaseTestCase.java (contents, props changed)
- copied, changed from r516480, incubator/tuscany/java/sca/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/InstanceWrapperBaseTestClass.java
incubator/tuscany/java/sca/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/ReflectiveInstanceWrapperTestCase.java (with props)
Removed:
incubator/tuscany/java/sca/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/InstanceWrapperBaseTestClass.java
Added: incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/component/ReflectiveInstanceFactory.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/component/ReflectiveInstanceFactory.java?view=auto&rev=516617
==============================================================================
--- incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/component/ReflectiveInstanceFactory.java (added)
+++ incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/component/ReflectiveInstanceFactory.java Fri Mar 9 16:10:21 2007
@@ -0,0 +1,83 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.
+ */
+package org.apache.tuscany.core.component;
+
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+
+import org.apache.tuscany.core.component.scope.InstanceWrapper;
+import org.apache.tuscany.core.component.scope.ReflectiveInstanceWrapper;
+import org.apache.tuscany.core.injection.EventInvoker;
+import org.apache.tuscany.core.injection.Injector;
+import org.apache.tuscany.spi.ObjectCreationException;
+import org.apache.tuscany.spi.ObjectFactory;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class ReflectiveInstanceFactory<T> implements InstanceFactory<T> {
+ private final Constructor<T> ctr;
+ private final ObjectFactory<?>[] ctrArgs;
+ private final Injector<T>[] injectors;
+ private final EventInvoker<T> initInvoker;
+ private final EventInvoker<T> destroyInvoker;
+
+ public ReflectiveInstanceFactory(Constructor<T> ctr,
+ ObjectFactory<?>[] ctrArgs,
+ Injector<T>[] injectors,
+ EventInvoker<T> initInvoker,
+ EventInvoker<T> destroyInvoker) {
+ this.ctr = ctr;
+ this.ctrArgs = ctrArgs;
+ this.injectors = injectors;
+ this.initInvoker = initInvoker;
+ this.destroyInvoker = destroyInvoker;
+ }
+
+ public InstanceWrapper<T> newInstance() {
+ T instance;
+ try {
+ if (ctrArgs != null) {
+ Object[] args = new Object[ctrArgs.length];
+ for (int i = 0; i < args.length; i++) {
+ args[i] = ctrArgs[i].getInstance();
+ }
+ instance = ctr.newInstance(args);
+ } else {
+ instance = ctr.newInstance();
+ }
+ } catch (InstantiationException e) {
+ String name = ctr.getDeclaringClass().getName();
+ throw new AssertionError("Class is not instantiable [" + name + "]");
+ } catch (IllegalAccessException e) {
+ String name = ctr.getName();
+ throw new AssertionError("Constructor is not accessible [" + name + "]");
+ } catch (
+ InvocationTargetException e) {
+ String name = ctr.getName();
+ throw new ObjectCreationException("Exception thrown by constructor", name, e);
+ }
+
+ for (Injector<T> injector : injectors) {
+ injector.inject(instance);
+ }
+
+ return new ReflectiveInstanceWrapper<T>(instance, initInvoker, destroyInvoker);
+ }
+}
Propchange: incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/component/ReflectiveInstanceFactory.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/component/ReflectiveInstanceFactory.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Added: incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/component/scope/ReflectiveInstanceWrapper.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/component/scope/ReflectiveInstanceWrapper.java?view=auto&rev=516617
==============================================================================
--- incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/component/scope/ReflectiveInstanceWrapper.java (added)
+++ incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/component/scope/ReflectiveInstanceWrapper.java Fri Mar 9 16:10:21 2007
@@ -0,0 +1,52 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.
+ */
+package org.apache.tuscany.core.component.scope;
+
+import org.apache.tuscany.spi.component.TargetInitializationException;
+import org.apache.tuscany.spi.component.TargetDestructionException;
+import org.apache.tuscany.core.injection.EventInvoker;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class ReflectiveInstanceWrapper<T> extends InstanceWrapperBase<T> {
+ private final EventInvoker<T> initInvoker;
+ private final EventInvoker<T> destroyInvoker;
+
+ public ReflectiveInstanceWrapper(T instance, EventInvoker<T> initInvoker, EventInvoker<T> destroyInvoker) {
+ super(instance);
+ this.initInvoker = initInvoker;
+ this.destroyInvoker = destroyInvoker;
+ }
+
+ public void start() throws TargetInitializationException {
+ if (initInvoker != null) {
+ initInvoker.invokeEvent(instance);
+ }
+ super.start();
+ }
+
+
+ public void stop() throws TargetDestructionException {
+ super.stop();
+ if (destroyInvoker != null) {
+ destroyInvoker.invokeEvent(instance);
+ }
+ }
+}
Propchange: incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/component/scope/ReflectiveInstanceWrapper.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/component/scope/ReflectiveInstanceWrapper.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Added: incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/component/SystemComponent.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/component/SystemComponent.java?view=auto&rev=516617
==============================================================================
--- incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/component/SystemComponent.java (added)
+++ incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/component/SystemComponent.java Fri Mar 9 16:10:21 2007
@@ -0,0 +1,40 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.
+ */
+package org.apache.tuscany.core.implementation.system.component;
+
+import org.apache.tuscany.core.component.InstanceFactory;
+import org.apache.tuscany.core.component.scope.InstanceWrapper;
+
+/**
+ * @version $Rev$ $Date$
+ * @param <T> the implementation class for the defined component
+ */
+public class SystemComponent<T> {
+ private InstanceFactory<T> instanceFactory;
+
+ public void start() {
+ }
+
+ public void stop() {
+ }
+
+ public InstanceWrapper<T> createInstance() {
+ return instanceFactory.newInstance();
+ }
+}
Propchange: incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/component/SystemComponent.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/component/SystemComponent.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Added: incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/model/SystemPhysicalComponentDefinition.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/model/SystemPhysicalComponentDefinition.java?view=auto&rev=516617
==============================================================================
--- incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/model/SystemPhysicalComponentDefinition.java (added)
+++ incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/model/SystemPhysicalComponentDefinition.java Fri Mar 9 16:10:21 2007
@@ -0,0 +1,31 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.
+ */
+package org.apache.tuscany.core.implementation.system.model;
+
+import org.apache.tuscany.spi.model.physical.PhysicalComponentDefinition;
+
+/**
+ * @version $Rev$ $Date$
+ * @param <T> the implementation class for the defined component
+ */
+public class SystemPhysicalComponentDefinition<T> extends PhysicalComponentDefinition {
+
+ // we can use an actual class as system components cannot be marshalled
+ private Class<T> implClass;
+}
Propchange: incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/model/SystemPhysicalComponentDefinition.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/tuscany/java/sca/kernel/core/src/main/java/org/apache/tuscany/core/implementation/system/model/SystemPhysicalComponentDefinition.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Copied: incubator/tuscany/java/sca/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/InstanceWrapperBaseTestCase.java (from r516480, incubator/tuscany/java/sca/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/InstanceWrapperBaseTestClass.java)
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/InstanceWrapperBaseTestCase.java?view=diff&rev=516617&p1=incubator/tuscany/java/sca/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/InstanceWrapperBaseTestClass.java&r1=516480&p2=incubator/tuscany/java/sca/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/InstanceWrapperBaseTestCase.java&r2=516617
==============================================================================
--- incubator/tuscany/java/sca/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/InstanceWrapperBaseTestClass.java (original)
+++ incubator/tuscany/java/sca/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/InstanceWrapperBaseTestCase.java Fri Mar 9 16:10:21 2007
@@ -23,7 +23,7 @@
/**
* @version $Rev$ $Date$
*/
-public class InstanceWrapperBaseTestClass extends TestCase {
+public class InstanceWrapperBaseTestCase extends TestCase {
private static final Object INSTANCE = new Object();
private InstanceWrapperBase<Object> wrapper;
Propchange: incubator/tuscany/java/sca/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/InstanceWrapperBaseTestCase.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/tuscany/java/sca/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/InstanceWrapperBaseTestCase.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Added: incubator/tuscany/java/sca/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/ReflectiveInstanceWrapperTestCase.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/ReflectiveInstanceWrapperTestCase.java?view=auto&rev=516617
==============================================================================
--- incubator/tuscany/java/sca/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/ReflectiveInstanceWrapperTestCase.java (added)
+++ incubator/tuscany/java/sca/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/ReflectiveInstanceWrapperTestCase.java Fri Mar 9 16:10:21 2007
@@ -0,0 +1,83 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.
+ */
+package org.apache.tuscany.core.component.scope;
+
+import junit.framework.TestCase;
+import static org.easymock.EasyMock.createMock;
+import org.easymock.EasyMock;
+
+import org.apache.tuscany.core.injection.EventInvoker;
+import org.apache.tuscany.spi.component.TargetDestructionException;
+import org.apache.tuscany.spi.component.TargetInitializationException;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class ReflectiveInstanceWrapperTestCase extends TestCase {
+ private ReflectiveInstanceWrapper<Object> wrapper;
+ private Object instance;
+ private EventInvoker<Object> initInvoker;
+ private EventInvoker<Object> destroyInvoker;
+
+ public void testWithNoCallbacks() {
+ wrapper = new ReflectiveInstanceWrapper<Object>(instance, null, null);
+ try {
+ wrapper.start();
+ } catch (TargetInitializationException e) {
+ fail();
+ }
+ try {
+ wrapper.stop();
+ } catch (TargetDestructionException e) {
+ fail();
+ }
+ }
+
+ public void testWithStartCallback() {
+ initInvoker.invokeEvent(instance);
+ EasyMock.replay(initInvoker);
+ wrapper = new ReflectiveInstanceWrapper<Object>(instance, initInvoker, null);
+ try {
+ wrapper.start();
+ } catch (TargetInitializationException e) {
+ fail();
+ }
+ EasyMock.verify(initInvoker);
+ }
+
+ public void testWithStopCallback() {
+ destroyInvoker.invokeEvent(instance);
+ EasyMock.replay(destroyInvoker);
+ wrapper = new ReflectiveInstanceWrapper<Object>(instance, null, destroyInvoker);
+ try {
+ wrapper.stop();
+ } catch (TargetDestructionException e) {
+ fail();
+ }
+ EasyMock.verify(destroyInvoker);
+ }
+
+ @SuppressWarnings("unchecked")
+ protected void setUp() throws Exception {
+ super.setUp();
+ instance = new Object();
+ initInvoker = createMock(EventInvoker.class);
+ destroyInvoker = createMock(EventInvoker.class);
+ }
+}
Propchange: incubator/tuscany/java/sca/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/ReflectiveInstanceWrapperTestCase.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/tuscany/java/sca/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/ReflectiveInstanceWrapperTestCase.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
---------------------------------------------------------------------
To unsubscribe, e-mail: tuscany-commits-unsubscribe@ws.apache.org
For additional commands, e-mail: tuscany-commits-help@ws.apache.org