Skip to content

Commit d701546

Browse files
committed
Wire up injection for dynamically constructed objects
1 parent 0debd8a commit d701546

9 files changed

Lines changed: 120 additions & 37 deletions

File tree

client/cloudstack-ui.launch

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,4 @@
11
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2-
<!--
3-
Licensed to the Apache Software Foundation (ASF) under one
4-
or more contributor license agreements. See the NOTICE file
5-
distributed with this work for additional information
6-
regarding copyright ownership. The ASF licenses this file
7-
to you under the Apache License, Version 2.0 (the
8-
"License"); you may not use this file except in compliance
9-
with the License. You may obtain a copy of the License at
10-
11-
http://www.apache.org/licenses/LICENSE-2.0
12-
13-
Unless required by applicable law or agreed to in writing,
14-
software distributed under the License is distributed on an
15-
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16-
KIND, either express or implied. See the License for the
17-
specific language governing permissions and limitations
18-
under the License.
19-
-->
202
<launchConfiguration type="org.eclipse.m2e.Maven2LaunchConfigurationType">
213
<booleanAttribute key="M2_DEBUG_OUTPUT" value="false"/>
224
<stringAttribute key="M2_GOALS" value="jetty:run"/>
@@ -28,5 +10,7 @@ under the License.
2810
<booleanAttribute key="M2_SKIP_TESTS" value="false"/>
2911
<booleanAttribute key="M2_UPDATE_SNAPSHOTS" value="false"/>
3012
<booleanAttribute key="M2_WORKSPACE_RESOLUTION" value="true"/>
13+
<stringAttribute key="org.eclipse.debug.core.source_locator_id" value="org.eclipse.m2e.launching.MavenSourceLocator"/>
14+
<stringAttribute key="org.eclipse.debug.core.source_locator_memento" value="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?&gt;&#10;&lt;sourceLookupDirector&gt;&#10;&lt;sourceContainers duplicates=&quot;false&quot;&gt;&#10;&lt;container memento=&quot;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot; standalone=&amp;quot;no&amp;quot;?&amp;gt;&amp;#10;&amp;lt;workspace/&amp;gt;&amp;#10;&quot; typeId=&quot;org.eclipse.debug.core.containerType.workspace&quot;/&gt;&#10;&lt;container memento=&quot;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot; standalone=&amp;quot;no&amp;quot;?&amp;gt;&amp;#10;&amp;lt;default/&amp;gt;&amp;#10;&quot; typeId=&quot;org.eclipse.debug.core.containerType.default&quot;/&gt;&#10;&lt;/sourceContainers&gt;&#10;&lt;/sourceLookupDirector&gt;&#10;"/>
3115
<stringAttribute key="org.eclipse.jdt.launching.WORKING_DIRECTORY" value="${project_loc:cloud-client-ui}"/>
3216
</launchConfiguration>
Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
1-
<?xml version="1.0" encoding="UTF-8"?>
21
<beans xmlns="http://www.springframework.org/schema/beans"
3-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xmlns:context="http://www.springframework.org/schema/context"
5-
xsi:schemaLocation="http://www.springframework.org/schema/beans
6-
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
7-
http://www.springframework.org/schema/context
8-
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns:context="http://www.springframework.org/schema/context"
4+
xmlns:tx="http://www.springframework.org/schema/tx"
5+
xmlns:aop="http://www.springframework.org/schema/aop"
6+
xsi:schemaLocation="http://www.springframework.org/schema/beans
7+
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
8+
http://www.springframework.org/schema/tx
9+
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
10+
http://www.springframework.org/schema/aop
11+
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
12+
http://www.springframework.org/schema/context
13+
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
14+
15+
<context:annotation-config />
16+
<context:component-scan base-package="org.apache.cloudstack, com.cloud" />
917

10-
<context:annotation-config />
11-
<context:component-scan base-package="com.cloud.cluster" />
1218
</beans>

server/src/com/cloud/server/ConfigurationServerImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public class ConfigurationServerImpl implements ConfigurationServer {
116116
private final NetworkOfferingDao _networkOfferingDao;
117117
private final DataCenterDao _dataCenterDao;
118118
private final NetworkDao _networkDao;
119-
private final VlanDao _vlanDao;
119+
private final VlanDao _vlanDao;
120120
private String _domainSuffix;
121121
private final DomainDao _domainDao;
122122
private final AccountDao _accountDao;

server/src/com/cloud/servlet/CloudStartupServlet.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public class CloudStartupServlet extends HttpServlet implements ServletContextLi
3939

4040
@Override
4141
public void init() throws ServletException {
42+
4243
// Save Configuration Values
4344
//ComponentLocator loc = ComponentLocator.getLocator(ConfigurationServer.Name);
4445
ConfigurationServer c = (ConfigurationServer)ComponentLocator.getComponent(ConfigurationServer.Name);

utils/src/com/cloud/utils/component/ComponentContext.java

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,51 @@
1717

1818
package com.cloud.utils.component;
1919

20+
import org.springframework.aop.Advisor;
21+
import org.springframework.aop.framework.ProxyFactory;
22+
import org.springframework.aop.support.DefaultPointcutAdvisor;
2023
import org.springframework.context.ApplicationContext;
24+
import org.springframework.context.ApplicationContextAware;
25+
import org.springframework.stereotype.Component;
26+
27+
import com.cloud.utils.db.TransactionContextBuilder;
2128

2229
/**
2330
*
2431
* ComponentContext.setApplication() and ComponentContext.getApplication()
2532
* are not recommended to be used outside, they exist to help wire Spring Framework
2633
*
2734
*/
28-
public class ComponentContext {
35+
@Component
36+
public class ComponentContext implements ApplicationContextAware {
2937
private static ApplicationContext s_appContext;
3038

31-
public static void setApplicationContext(ApplicationContext applicationContext) {
39+
public void setApplicationContext(ApplicationContext applicationContext) {
3240
s_appContext = applicationContext;
3341
}
3442

3543
public static ApplicationContext getApplicationContext() {
3644
return s_appContext;
3745
}
3846

39-
public <T> T getCompanent(String name) {
47+
public static <T> T getCompanent(String name) {
4048
assert(s_appContext != null);
4149
return (T)s_appContext.getBean(name);
4250
}
51+
52+
public static <T> T getCompanent(Class<T> beanType) {
53+
assert(s_appContext != null);
54+
return (T)s_appContext.getBean(beanType);
55+
}
56+
57+
public static<T> T inject(Object instance) {
58+
Advisor advisor = new DefaultPointcutAdvisor(new MatchAnyMethodPointcut(),
59+
new TransactionContextBuilder());
60+
ProxyFactory pf = new ProxyFactory();
61+
62+
pf.setTarget(instance);
63+
pf.addAdvisor(advisor);
64+
65+
return (T)pf.getProxy();
66+
}
4367
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
package com.cloud.utils.component;
18+
19+
import java.lang.reflect.Method;
20+
21+
import org.springframework.aop.support.StaticMethodMatcherPointcut;
22+
23+
public class MatchAnyMethodPointcut extends StaticMethodMatcherPointcut {
24+
public boolean matches(Method method, Class<?> cls) {
25+
return true;
26+
}
27+
}

utils/src/com/cloud/utils/db/TransactionContextBuilder.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@
1818

1919
import java.lang.reflect.Method;
2020

21+
import org.aopalliance.intercept.MethodInterceptor;
22+
import org.aopalliance.intercept.MethodInvocation;
2123
import org.aspectj.lang.ProceedingJoinPoint;
2224
import org.aspectj.lang.reflect.MethodSignature;
2325

24-
public class TransactionContextBuilder {
26+
public class TransactionContextBuilder implements MethodInterceptor {
2527
public TransactionContextBuilder() {
2628
}
2729

@@ -40,6 +42,23 @@ public Object AroundAnyMethod(ProceedingJoinPoint call) throws Throwable {
4042
}
4143
return call.proceed();
4244
}
45+
46+
@Override
47+
public Object invoke(MethodInvocation method) throws Throwable {
48+
Method targetMethod = method.getMethod();
49+
50+
if(needToIntercept(targetMethod)) {
51+
Transaction txn = Transaction.open(targetMethod.getName());
52+
Object ret = null;
53+
try {
54+
ret = method.proceed();
55+
} finally {
56+
txn.close();
57+
}
58+
return ret;
59+
}
60+
return method.proceed();
61+
}
4362

4463
private boolean needToIntercept(Method method) {
4564
DB db = method.getAnnotation(DB.class);

utils/test/com/cloud/utils/QualifierTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,18 @@
1616
// under the License.
1717
package com.cloud.utils;
1818

19+
import javax.inject.Inject;
20+
1921
import org.junit.Test;
2022
import org.junit.runner.RunWith;
21-
import org.springframework.beans.factory.annotation.Autowired;
2223
import org.springframework.test.context.ContextConfiguration;
2324
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
2425

2526
@RunWith(SpringJUnit4ClassRunner.class)
2627
@ContextConfiguration(locations="classpath:/com/cloud/utils/QualifierTestContext.xml")
2728
public class QualifierTest {
2829

29-
@Autowired
30+
@Inject
3031
DummyInterface _dummy;
3132

3233
@Test
Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
117
package com.cloud.utils.db;
218

319
import javax.inject.Inject;
@@ -7,10 +23,11 @@
723
import org.springframework.test.context.ContextConfiguration;
824
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
925

26+
import com.cloud.utils.component.ComponentContext;
27+
1028
@RunWith(SpringJUnit4ClassRunner.class)
1129
@ContextConfiguration(locations="classpath:/com/cloud/utils/db/transactioncontextBuilderTest.xml")
1230
public class TransactionContextBuilderTest {
13-
1431
@Inject
1532
DbAnnotatedBaseDerived _derived;
1633

@@ -19,7 +36,11 @@ public class TransactionContextBuilderTest {
1936

2037
@Test
2138
public void test() {
22-
_derived.DbAnnotatedMethod();
23-
_base.MethodWithClassDbAnnotated();
39+
// _derived.DbAnnotatedMethod();
40+
// _base.MethodWithClassDbAnnotated();
41+
42+
// test @DB injection on dynamically constructed objects
43+
DbAnnotatedBase base = ComponentContext.inject(new DbAnnotatedBase());
44+
base.MethodWithClassDbAnnotated();
2445
}
2546
}

0 commit comments

Comments
 (0)