View Javadoc

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          super();
53          
54          this.containerConfigValue   = "./conf/containerConfiguration.xml";
55          this.applicationHome        = ".";
56          this.tempHome               = System.getProperty("java.io.tmpdir",".");
57      }
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          ServiceContainerConfiguration config = new ServiceContainerConfiguration();
73  
74          // wrap Spring's BeanFactory to allow service lookups
75          BeanFactoryServiceManager beanFactoryServiceManager = new BeanFactoryServiceManager(this.getBeanFactory());
76  
77          // intialize the Avalon serviceContainer
78          config.setLogger( this.getLogger() );
79          config.setApplicationRootDir( this.getApplicationHome() );
80          config.setTempRootDir( this.getTempHome() );
81          config.loadContainerConfiguration( this.getContainerConfigValue(), "auto" );
82          config.setParentServiceManager(beanFactoryServiceManager);
83          config.setContext(new DefaultContext(this.getDefaultContext()));
84  
85          this.setServiceManager(ServiceContainerFactory.create(config));
86      }
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          if( this.getServiceManager() == null)
97          {
98              return;
99          }
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 }