Updated Branches: refs/heads/javelin 0debd8a39 -> d70154609
Wire up injection for dynamically constructed objects Project: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/commit/d7015460 Tree: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/tree/d7015460 Diff: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/diff/d7015460 Branch: refs/heads/javelin Commit: d70154609a87909458d5ce092490b55a83e14a2a Parents: 0debd8a Author: Kelven Yang <[email protected]> Authored: Mon Oct 29 16:15:52 2012 -0700 Committer: Kelven Yang <[email protected]> Committed: Mon Oct 29 16:16:07 2012 -0700 ---------------------------------------------------------------------- client/cloudstack-ui.launch | 20 +--------- client/tomcatconf/applicationContext.xml.in | 24 +++++++---- .../com/cloud/server/ConfigurationServerImpl.java | 2 +- .../src/com/cloud/servlet/CloudStartupServlet.java | 1 + .../cloud/utils/component/ComponentContext.java | 30 +++++++++++++- .../utils/component/MatchAnyMethodPointcut.java | 27 +++++++++++++ .../cloud/utils/db/TransactionContextBuilder.java | 21 ++++++++++- utils/test/com/cloud/utils/QualifierTest.java | 5 +- .../utils/db/TransactionContextBuilderTest.java | 27 ++++++++++++- 9 files changed, 120 insertions(+), 37 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/d7015460/client/cloudstack-ui.launch ---------------------------------------------------------------------- diff --git a/client/cloudstack-ui.launch b/client/cloudstack-ui.launch index 1943d17..0f09a5e 100644 --- a/client/cloudstack-ui.launch +++ b/client/cloudstack-ui.launch @@ -1,22 +1,4 @@ <?xml version="1.0" encoding="UTF-8" standalone="no"?> -<!-- -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. ---> <launchConfiguration type="org.eclipse.m2e.Maven2LaunchConfigurationType"> <booleanAttribute key="M2_DEBUG_OUTPUT" value="false"/> <stringAttribute key="M2_GOALS" value="jetty:run"/> @@ -28,5 +10,7 @@ under the License. <booleanAttribute key="M2_SKIP_TESTS" value="false"/> <booleanAttribute key="M2_UPDATE_SNAPSHOTS" value="false"/> <booleanAttribute key="M2_WORKSPACE_RESOLUTION" value="true"/> +<stringAttribute key="org.eclipse.debug.core.source_locator_id" value="org.eclipse.m2e.launching.MavenSourceLocator"/> +<stringAttribute key="org.eclipse.debug.core.source_locator_memento" value="<?xml version="1.0" encoding="UTF-8" standalone="no"?> <sourceLookupDirector> <sourceContainers duplicates="false"> <container memento="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?&gt;&#10;&lt;workspace/&gt;&#10;" typeId="org.eclipse.debug.core.containerType.workspace"/> <container memento="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?&gt;&#10;&lt;default/&gt;&#10;" typeId="org.eclipse.debug.core.containerType.default"/> </sourceContainers> </sourceLookupDirector> "/> <stringAttribute key="org.eclipse.jdt.launching.WORKING_DIRECTORY" value="${project_loc:cloud-client-ui}"/> </launchConfiguration> http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/d7015460/client/tomcatconf/applicationContext.xml.in ---------------------------------------------------------------------- diff --git a/client/tomcatconf/applicationContext.xml.in b/client/tomcatconf/applicationContext.xml.in index 6acd9b3..101d43b 100644 --- a/client/tomcatconf/applicationContext.xml.in +++ b/client/tomcatconf/applicationContext.xml.in @@ -1,12 +1,18 @@ -<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xmlns:context="http://www.springframework.org/schema/context" - xsi:schemaLocation="http://www.springframework.org/schema/beans - http://www.springframework.org/schema/beans/spring-beans-3.0.xsd - http://www.springframework.org/schema/context - http://www.springframework.org/schema/context/spring-context-3.0.xsd"> + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xmlns:context="http://www.springframework.org/schema/context" + xmlns:tx="http://www.springframework.org/schema/tx" + xmlns:aop="http://www.springframework.org/schema/aop" + xsi:schemaLocation="http://www.springframework.org/schema/beans + http://www.springframework.org/schema/beans/spring-beans-3.0.xsd + http://www.springframework.org/schema/tx + http://www.springframework.org/schema/tx/spring-tx-3.0.xsd + http://www.springframework.org/schema/aop + http://www.springframework.org/schema/aop/spring-aop-3.0.xsd + http://www.springframework.org/schema/context + http://www.springframework.org/schema/context/spring-context-3.0.xsd"> + + <context:annotation-config /> + <context:component-scan base-package="org.apache.cloudstack, com.cloud" /> - <context:annotation-config /> - <context:component-scan base-package="com.cloud.cluster" /> </beans> http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/d7015460/server/src/com/cloud/server/ConfigurationServerImpl.java ---------------------------------------------------------------------- diff --git a/server/src/com/cloud/server/ConfigurationServerImpl.java b/server/src/com/cloud/server/ConfigurationServerImpl.java index a0d10b6..28893da 100755 --- a/server/src/com/cloud/server/ConfigurationServerImpl.java +++ b/server/src/com/cloud/server/ConfigurationServerImpl.java @@ -116,7 +116,7 @@ public class ConfigurationServerImpl implements ConfigurationServer { private final NetworkOfferingDao _networkOfferingDao; private final DataCenterDao _dataCenterDao; private final NetworkDao _networkDao; - private final VlanDao _vlanDao; + private final VlanDao _vlanDao; private String _domainSuffix; private final DomainDao _domainDao; private final AccountDao _accountDao; http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/d7015460/server/src/com/cloud/servlet/CloudStartupServlet.java ---------------------------------------------------------------------- diff --git a/server/src/com/cloud/servlet/CloudStartupServlet.java b/server/src/com/cloud/servlet/CloudStartupServlet.java index c3e5a82..e38b923 100755 --- a/server/src/com/cloud/servlet/CloudStartupServlet.java +++ b/server/src/com/cloud/servlet/CloudStartupServlet.java @@ -39,6 +39,7 @@ public class CloudStartupServlet extends HttpServlet implements ServletContextLi @Override public void init() throws ServletException { + // Save Configuration Values //ComponentLocator loc = ComponentLocator.getLocator(ConfigurationServer.Name); ConfigurationServer c = (ConfigurationServer)ComponentLocator.getComponent(ConfigurationServer.Name); http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/d7015460/utils/src/com/cloud/utils/component/ComponentContext.java ---------------------------------------------------------------------- diff --git a/utils/src/com/cloud/utils/component/ComponentContext.java b/utils/src/com/cloud/utils/component/ComponentContext.java index 153c500..91990d4 100644 --- a/utils/src/com/cloud/utils/component/ComponentContext.java +++ b/utils/src/com/cloud/utils/component/ComponentContext.java @@ -17,7 +17,14 @@ package com.cloud.utils.component; +import org.springframework.aop.Advisor; +import org.springframework.aop.framework.ProxyFactory; +import org.springframework.aop.support.DefaultPointcutAdvisor; import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; +import org.springframework.stereotype.Component; + +import com.cloud.utils.db.TransactionContextBuilder; /** * @@ -25,10 +32,11 @@ import org.springframework.context.ApplicationContext; * are not recommended to be used outside, they exist to help wire Spring Framework * */ -public class ComponentContext { +@Component +public class ComponentContext implements ApplicationContextAware { private static ApplicationContext s_appContext; - public static void setApplicationContext(ApplicationContext applicationContext) { + public void setApplicationContext(ApplicationContext applicationContext) { s_appContext = applicationContext; } @@ -36,8 +44,24 @@ public class ComponentContext { return s_appContext; } - public <T> T getCompanent(String name) { + public static <T> T getCompanent(String name) { assert(s_appContext != null); return (T)s_appContext.getBean(name); } + + public static <T> T getCompanent(Class<T> beanType) { + assert(s_appContext != null); + return (T)s_appContext.getBean(beanType); + } + + public static<T> T inject(Object instance) { + Advisor advisor = new DefaultPointcutAdvisor(new MatchAnyMethodPointcut(), + new TransactionContextBuilder()); + ProxyFactory pf = new ProxyFactory(); + + pf.setTarget(instance); + pf.addAdvisor(advisor); + + return (T)pf.getProxy(); + } } http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/d7015460/utils/src/com/cloud/utils/component/MatchAnyMethodPointcut.java ---------------------------------------------------------------------- diff --git a/utils/src/com/cloud/utils/component/MatchAnyMethodPointcut.java b/utils/src/com/cloud/utils/component/MatchAnyMethodPointcut.java new file mode 100644 index 0000000..dd59734 --- /dev/null +++ b/utils/src/com/cloud/utils/component/MatchAnyMethodPointcut.java @@ -0,0 +1,27 @@ +// 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 +// 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 com.cloud.utils.component; + +import java.lang.reflect.Method; + +import org.springframework.aop.support.StaticMethodMatcherPointcut; + +public class MatchAnyMethodPointcut extends StaticMethodMatcherPointcut { + public boolean matches(Method method, Class<?> cls) { + return true; + } +} http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/d7015460/utils/src/com/cloud/utils/db/TransactionContextBuilder.java ---------------------------------------------------------------------- diff --git a/utils/src/com/cloud/utils/db/TransactionContextBuilder.java b/utils/src/com/cloud/utils/db/TransactionContextBuilder.java index 054ddcd..8f70087 100644 --- a/utils/src/com/cloud/utils/db/TransactionContextBuilder.java +++ b/utils/src/com/cloud/utils/db/TransactionContextBuilder.java @@ -18,10 +18,12 @@ package com.cloud.utils.db; import java.lang.reflect.Method; +import org.aopalliance.intercept.MethodInterceptor; +import org.aopalliance.intercept.MethodInvocation; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.reflect.MethodSignature; -public class TransactionContextBuilder { +public class TransactionContextBuilder implements MethodInterceptor { public TransactionContextBuilder() { } @@ -40,6 +42,23 @@ public class TransactionContextBuilder { } return call.proceed(); } + + @Override + public Object invoke(MethodInvocation method) throws Throwable { + Method targetMethod = method.getMethod(); + + if(needToIntercept(targetMethod)) { + Transaction txn = Transaction.open(targetMethod.getName()); + Object ret = null; + try { + ret = method.proceed(); + } finally { + txn.close(); + } + return ret; + } + return method.proceed(); + } private boolean needToIntercept(Method method) { DB db = method.getAnnotation(DB.class); http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/d7015460/utils/test/com/cloud/utils/QualifierTest.java ---------------------------------------------------------------------- diff --git a/utils/test/com/cloud/utils/QualifierTest.java b/utils/test/com/cloud/utils/QualifierTest.java index 10c0e81..8d758a5 100644 --- a/utils/test/com/cloud/utils/QualifierTest.java +++ b/utils/test/com/cloud/utils/QualifierTest.java @@ -16,9 +16,10 @@ // under the License. package com.cloud.utils; +import javax.inject.Inject; + import org.junit.Test; import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @@ -26,7 +27,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @ContextConfiguration(locations="classpath:/com/cloud/utils/QualifierTestContext.xml") public class QualifierTest { - @Autowired + @Inject DummyInterface _dummy; @Test http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/d7015460/utils/test/com/cloud/utils/db/TransactionContextBuilderTest.java ---------------------------------------------------------------------- diff --git a/utils/test/com/cloud/utils/db/TransactionContextBuilderTest.java b/utils/test/com/cloud/utils/db/TransactionContextBuilderTest.java index ca9d47a..bf1fbeb 100644 --- a/utils/test/com/cloud/utils/db/TransactionContextBuilderTest.java +++ b/utils/test/com/cloud/utils/db/TransactionContextBuilderTest.java @@ -1,3 +1,19 @@ +// 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 +// 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 com.cloud.utils.db; import javax.inject.Inject; @@ -7,10 +23,11 @@ import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import com.cloud.utils.component.ComponentContext; + @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations="classpath:/com/cloud/utils/db/transactioncontextBuilderTest.xml") public class TransactionContextBuilderTest { - @Inject DbAnnotatedBaseDerived _derived; @@ -19,7 +36,11 @@ public class TransactionContextBuilderTest { @Test public void test() { - _derived.DbAnnotatedMethod(); - _base.MethodWithClassDbAnnotated(); + // _derived.DbAnnotatedMethod(); + // _base.MethodWithClassDbAnnotated(); + + // test @DB injection on dynamically constructed objects + DbAnnotatedBase base = ComponentContext.inject(new DbAnnotatedBase()); + base.MethodWithClassDbAnnotated(); } }
