Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
YaafiContainerBean |
|
| 1.3;1.3 |
1 | package org.apache.fulcrum.spring; | |
2 | ||
3 | /* | |
4 | * Licensed to the Apache Software Foundation (ASF) under one | |
5 | * or more contributor license agreements. See the NOTICE file | |
6 | * distributed with this work for additional information | |
7 | * regarding copyright ownership. The ASF licenses this file | |
8 | * to you under the Apache License, Version 2.0 (the | |
9 | * "License"); you may not use this file except in compliance | |
10 | * with the License. You may obtain a copy of the License at | |
11 | * | |
12 | * http://www.apache.org/licenses/LICENSE-2.0 | |
13 | * | |
14 | * Unless required by applicable law or agreed to in writing, | |
15 | * software distributed under the License is distributed on an | |
16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
17 | * KIND, either express or implied. See the License for the | |
18 | * specific language governing permissions and limitations | |
19 | * under the License. | |
20 | */ | |
21 | ||
22 | import java.io.File; | |
23 | ||
24 | import org.apache.avalon.framework.activity.Disposable; | |
25 | import org.apache.avalon.framework.context.DefaultContext; | |
26 | import org.apache.fulcrum.yaafi.framework.factory.ServiceContainerConfiguration; | |
27 | import org.apache.fulcrum.yaafi.framework.factory.ServiceContainerFactory; | |
28 | ||
29 | /** | |
30 | * A POJO starting/stopping the YAAFI container and exposing a ServiceManager. | |
31 | * This allows to run an Avalon container within Spring and to lookup Avalon | |
32 | * services using the exposed ServiceManager. | |
33 | * | |
34 | * @author <a href="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl</a> | |
35 | */ | |
36 | public class YaafiContainerBean extends AvalonContainerBean | |
37 | { | |
38 | /** The location of the container configuration */ | |
39 | private String containerConfigValue; | |
40 | ||
41 | /** the working directory */ | |
42 | private String applicationHome; | |
43 | ||
44 | /** the temp directory */ | |
45 | private String tempHome; | |
46 | ||
47 | /** | |
48 | * Constructor | |
49 | */ | |
50 | public YaafiContainerBean() | |
51 | { | |
52 | 0 | super(); |
53 | ||
54 | 0 | this.containerConfigValue = "./conf/containerConfiguration.xml"; |
55 | 0 | this.applicationHome = "."; |
56 | 0 | this.tempHome = System.getProperty("java.io.tmpdir","."); |
57 | 0 | } |
58 | ||
59 | ///////////////////////////////////////////////////////////////////////// | |
60 | // Interface Implementation | |
61 | ///////////////////////////////////////////////////////////////////////// | |
62 | ||
63 | /** | |
64 | * Initialize the instance. This method must be configured using | |
65 | * the 'init-method' attribute. | |
66 | * | |
67 | * @see org.apache.avalon.framework.activity.Initializable#initialize() | |
68 | * @throws Exception the initialization failed | |
69 | */ | |
70 | public void initialize() throws Exception | |
71 | { | |
72 | 0 | ServiceContainerConfiguration config = new ServiceContainerConfiguration(); |
73 | ||
74 | // wrap Spring's BeanFactory to allow service lookups | |
75 | 0 | BeanFactoryServiceManager beanFactoryServiceManager = new BeanFactoryServiceManager(this.getBeanFactory()); |
76 | ||
77 | // intialize the Avalon serviceContainer | |
78 | 0 | config.setLogger( this.getLogger() ); |
79 | 0 | config.setApplicationRootDir( this.getApplicationHome() ); |
80 | 0 | config.setTempRootDir( this.getTempHome() ); |
81 | 0 | config.loadContainerConfiguration( this.getContainerConfigValue(), "auto" ); |
82 | 0 | config.setParentServiceManager(beanFactoryServiceManager); |
83 | 0 | config.setContext(new DefaultContext(this.getDefaultContext())); |
84 | ||
85 | 0 | this.setServiceManager(ServiceContainerFactory.create(config)); |
86 | 0 | } |
87 | ||
88 | /** | |
89 | * Dispose the YAAFI container. This method must be configured using | |
90 | * the 'destroy-method' attribute. | |
91 | * | |
92 | * @see org.apache.avalon.framework.activity.Disposable#dispose() | |
93 | */ | |
94 | public void dispose() | |
95 | { | |
96 | 0 | if( this.getServiceManager() == null) |
97 | { | |
98 | 0 | return; |
99 | } | |
100 | ||
101 | try | |
102 | { | |
103 | // dispose the service serviceContainer | |
104 | 0 | ((Disposable) this.getServiceManager()).dispose(); |
105 | } | |
106 | 0 | catch (Exception e) |
107 | { | |
108 | 0 | String msg = "Failed to terminate " + this.getClass().getName(); |
109 | 0 | this.getLogger().error(msg,e); |
110 | } | |
111 | finally { | |
112 | 0 | this.setServiceManager(null); |
113 | 0 | } |
114 | 0 | } |
115 | ||
116 | ///////////////////////////////////////////////////////////////////////// | |
117 | // Generated getters & setters | |
118 | ///////////////////////////////////////////////////////////////////////// | |
119 | ||
120 | /** | |
121 | * @return Returns the applicationHome. | |
122 | */ | |
123 | public String getApplicationHome() | |
124 | { | |
125 | 0 | return this.applicationHome; |
126 | } | |
127 | ||
128 | /** | |
129 | * @param applicationHome The applicationHome to set. | |
130 | */ | |
131 | public void setApplicationHome(String applicationHome) | |
132 | { | |
133 | 0 | this.applicationHome = applicationHome; |
134 | 0 | } |
135 | ||
136 | /** | |
137 | * @return Returns the containerConfigValue. | |
138 | */ | |
139 | public String getContainerConfigValue() | |
140 | { | |
141 | 0 | return containerConfigValue; |
142 | } | |
143 | ||
144 | /** | |
145 | * @param containerConfigValue The containerConfigValue to set. | |
146 | */ | |
147 | public void setContainerConfigValue(String containerConfigValue) | |
148 | { | |
149 | 0 | this.containerConfigValue = containerConfigValue; |
150 | 0 | } |
151 | ||
152 | /** | |
153 | * @return Returns the tempHome. | |
154 | */ | |
155 | public String getTempHome() | |
156 | { | |
157 | 0 | return this.tempHome; |
158 | } | |
159 | ||
160 | /** | |
161 | * @param tempHome The tempHome to set. | |
162 | */ | |
163 | public void setTempHome(String tempHome) | |
164 | { | |
165 | 0 | this.tempHome = tempHome; |
166 | 0 | } |
167 | ||
168 | ///////////////////////////////////////////////////////////////////////// | |
169 | // Implementation | |
170 | ///////////////////////////////////////////////////////////////////////// | |
171 | ||
172 | /** | |
173 | * @see Object#toString() | |
174 | */ | |
175 | public String toString() | |
176 | { | |
177 | 0 | StringBuffer result = new StringBuffer(); |
178 | 0 | result.append(getClass().getName()).append("@").append(Integer.toHexString(hashCode())); |
179 | 0 | result.append('['); |
180 | 0 | result.append("beanName=").append(this.getBeanName()); |
181 | 0 | result.append(','); |
182 | 0 | result.append("workingDir=").append(new File("").getAbsolutePath()); |
183 | 0 | result.append(','); |
184 | 0 | result.append("applicationHome=").append(this.getApplicationHome()); |
185 | 0 | result.append(','); |
186 | 0 | result.append("tempHome=").append(this.getTempHome()); |
187 | 0 | result.append(','); |
188 | 0 | result.append("logger=").append(this.getLogger().getClass().getName()); |
189 | 0 | result.append(','); |
190 | 0 | result.append("containerConfigValue=").append(this.getContainerConfigValue()); |
191 | 0 | result.append(']'); |
192 | 0 | return result.toString(); |
193 | } | |
194 | } |