001 package org.apache.fulcrum.spring;
002
003 /*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements. See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership. The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License. You may obtain a copy of the License at
011 *
012 * http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied. See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022 import java.io.File;
023
024 import org.apache.avalon.framework.activity.Disposable;
025 import org.apache.avalon.framework.context.DefaultContext;
026 import org.apache.fulcrum.yaafi.framework.factory.ServiceContainerConfiguration;
027 import org.apache.fulcrum.yaafi.framework.factory.ServiceContainerFactory;
028
029 /**
030 * A POJO starting/stopping the YAAFI container and exposing a ServiceManager.
031 * This allows to run an Avalon container within Spring and to lookup Avalon
032 * services using the exposed ServiceManager.
033 *
034 * @author <a href="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl</a>
035 */
036 public class YaafiContainerBean extends AvalonContainerBean
037 {
038 /** The location of the container configuration */
039 private String containerConfigValue;
040
041 /** the working directory */
042 private String applicationHome;
043
044 /** the temp directory */
045 private String tempHome;
046
047 /**
048 * Constructor
049 */
050 public YaafiContainerBean()
051 {
052 super();
053
054 this.containerConfigValue = "./conf/containerConfiguration.xml";
055 this.applicationHome = ".";
056 this.tempHome = System.getProperty("java.io.tmpdir",".");
057 }
058
059 /////////////////////////////////////////////////////////////////////////
060 // Interface Implementation
061 /////////////////////////////////////////////////////////////////////////
062
063 /**
064 * Initialize the instance. This method must be configured using
065 * the 'init-method' attribute.
066 *
067 * @see org.apache.avalon.framework.activity.Initializable#initialize()
068 * @throws Exception the initialization failed
069 */
070 public void initialize() throws Exception
071 {
072 ServiceContainerConfiguration config = new ServiceContainerConfiguration();
073
074 // wrap Spring's BeanFactory to allow service lookups
075 BeanFactoryServiceManager beanFactoryServiceManager = new BeanFactoryServiceManager(this.getBeanFactory());
076
077 // intialize the Avalon serviceContainer
078 config.setLogger( this.getLogger() );
079 config.setApplicationRootDir( this.getApplicationHome() );
080 config.setTempRootDir( this.getTempHome() );
081 config.loadContainerConfiguration( this.getContainerConfigValue(), "auto" );
082 config.setParentServiceManager(beanFactoryServiceManager);
083 config.setContext(new DefaultContext(this.getDefaultContext()));
084
085 this.setServiceManager(ServiceContainerFactory.create(config));
086 }
087
088 /**
089 * Dispose the YAAFI container. This method must be configured using
090 * the 'destroy-method' attribute.
091 *
092 * @see org.apache.avalon.framework.activity.Disposable#dispose()
093 */
094 public void dispose()
095 {
096 if( this.getServiceManager() == null)
097 {
098 return;
099 }
100
101 try
102 {
103 // dispose the service serviceContainer
104 ((Disposable) this.getServiceManager()).dispose();
105 }
106 catch (Exception e)
107 {
108 String msg = "Failed to terminate " + this.getClass().getName();
109 this.getLogger().error(msg,e);
110 }
111 finally {
112 this.setServiceManager(null);
113 }
114 }
115
116 /////////////////////////////////////////////////////////////////////////
117 // Generated getters & setters
118 /////////////////////////////////////////////////////////////////////////
119
120 /**
121 * @return Returns the applicationHome.
122 */
123 public String getApplicationHome()
124 {
125 return this.applicationHome;
126 }
127
128 /**
129 * @param applicationHome The applicationHome to set.
130 */
131 public void setApplicationHome(String applicationHome)
132 {
133 this.applicationHome = applicationHome;
134 }
135
136 /**
137 * @return Returns the containerConfigValue.
138 */
139 public String getContainerConfigValue()
140 {
141 return containerConfigValue;
142 }
143
144 /**
145 * @param containerConfigValue The containerConfigValue to set.
146 */
147 public void setContainerConfigValue(String containerConfigValue)
148 {
149 this.containerConfigValue = containerConfigValue;
150 }
151
152 /**
153 * @return Returns the tempHome.
154 */
155 public String getTempHome()
156 {
157 return this.tempHome;
158 }
159
160 /**
161 * @param tempHome The tempHome to set.
162 */
163 public void setTempHome(String tempHome)
164 {
165 this.tempHome = tempHome;
166 }
167
168 /////////////////////////////////////////////////////////////////////////
169 // Implementation
170 /////////////////////////////////////////////////////////////////////////
171
172 /**
173 * @see Object#toString()
174 */
175 public String toString()
176 {
177 StringBuffer result = new StringBuffer();
178 result.append(getClass().getName()).append("@").append(Integer.toHexString(hashCode()));
179 result.append('[');
180 result.append("beanName=").append(this.getBeanName());
181 result.append(',');
182 result.append("workingDir=").append(new File("").getAbsolutePath());
183 result.append(',');
184 result.append("applicationHome=").append(this.getApplicationHome());
185 result.append(',');
186 result.append("tempHome=").append(this.getTempHome());
187 result.append(',');
188 result.append("logger=").append(this.getLogger().getClass().getName());
189 result.append(',');
190 result.append("containerConfigValue=").append(this.getContainerConfigValue());
191 result.append(']');
192 return result.toString();
193 }
194 }