View Javadoc

1   package org.apache.torque.avalon;
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.Initializable;
25  import org.apache.avalon.framework.activity.Startable;
26  import org.apache.avalon.framework.configuration.Configurable;
27  import org.apache.avalon.framework.configuration.Configuration;
28  import org.apache.avalon.framework.configuration.ConfigurationException;
29  import org.apache.avalon.framework.context.Context;
30  import org.apache.avalon.framework.context.ContextException;
31  import org.apache.avalon.framework.context.Contextualizable;
32  import org.apache.avalon.framework.logger.LogEnabled;
33  import org.apache.avalon.framework.logger.Logger;
34  import org.apache.avalon.framework.thread.ThreadSafe;
35  import org.apache.commons.lang.StringUtils;
36  import org.apache.torque.TorqueInstance;
37  
38  /***
39   * Avalon component for Torque.
40   *
41   * @author <a href="mailto:mpoeschl@marmot.at">Martin Poeschl</a>
42   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
43   * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a>
44   * @version $Id: TorqueComponent.java 476550 2006-11-18 16:08:37Z tfischer $
45   */
46  public class TorqueComponent
47          extends TorqueInstance
48          implements Torque,
49                     LogEnabled,
50                     Configurable,
51                     Initializable,
52                     Contextualizable,
53                     Startable,
54                     ThreadSafe
55  {
56      /*** The Avalon Application Root */
57      private String appRoot = null;
58  
59      /*** The Avalon Logger */
60      private Logger logger = null;
61  
62      /*** The configuration file name. */
63      private String configFile = null;
64  
65  
66      /***
67       * Creates a new instance.  Default constructor used by Avalon.
68       */
69      public TorqueComponent()
70      {
71          super();
72  
73          // Provide the singleton instance to the static accessor
74          org.apache.torque.Torque.setInstance(this);
75      }
76  
77      /*
78       * ========================================================================
79       *
80       * Avalon Component Interfaces
81       *
82       * ========================================================================
83       */
84  
85      /***
86       * @see org.apache.avalon.framework.logger.LogEnabled#enableLogging(org.apache.avalon.framework.logger.Logger)
87       */
88      public void enableLogging(Logger aLogger)
89      {
90          this.logger = aLogger;
91      }
92  
93      /***
94       * Convenience method to provide the Avalon logger the way AbstractLogEnabled does.
95       */
96      public Logger getLogger()
97      {
98          return logger;
99      }
100 
101     /***
102      * @see
103      * org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration)
104      */
105     public void configure(Configuration configuration)
106             throws ConfigurationException
107     {
108         getLogger().debug("configure(" + configuration + ")");
109 
110         String configurationFile
111                 = configuration.getChild("configfile").getValue();
112 
113         if (StringUtils.isNotEmpty(appRoot))
114         {
115             if (configurationFile.startsWith("/"))
116             {
117                 configurationFile = configurationFile.substring(1);
118                 getLogger().debug("Config File changes to "
119                         + configurationFile);
120             }
121 
122             StringBuffer sb = new StringBuffer();
123             sb.append(appRoot);
124             sb.append(File.separator);
125             sb.append(configurationFile);
126 
127             configurationFile = sb.toString();
128         }
129 
130         getLogger().debug("Config File is " + configurationFile);
131 
132         this.configFile = configurationFile;
133     }
134 
135     /***
136      * @see org.apache.avalon.framework.context.Contextualizable
137      */
138     public void contextualize(Context context)
139             throws ContextException
140     {
141         // check context Merlin and YAAFI style
142         try
143         {
144             appRoot = ((File) context.get("urn:avalon:home")).getAbsolutePath();
145         }
146         catch (ContextException ce)
147         {
148             appRoot = null;
149         }
150 
151         if (appRoot == null)
152         {
153             // check context old ECM style, let exception flow if not available
154             appRoot = (String) context.get("componentAppRoot");
155         }
156 
157         if (StringUtils.isNotEmpty(appRoot))
158         {
159             if (appRoot.endsWith("/"))
160             {
161                 appRoot = appRoot.substring(0, appRoot.length() - 1);
162                 getLogger().debug("Application Root changed to " + appRoot);
163             }
164         }
165     }
166 
167     /***
168      * @see org.apache.avalon.framework.activity.Initializable#initialize()
169      */
170     public void initialize()
171             throws Exception
172     {
173         getLogger().debug("initialize()");
174         init(configFile);
175     }
176 
177     /***
178      * @see org.apache.avalon.framework.activity.Startable#start()
179      */
180     public void start()
181     {
182         getLogger().debug("start()");
183     }
184 
185     /***
186      * @see org.apache.avalon.framework.activity.Startable#stop()
187      */
188     public void stop()
189     {
190         getLogger().debug("stop()");
191         try
192         {
193             shutdown();
194         }
195         catch (Exception e)
196         {
197             getLogger().error("Error while stopping Torque", e);
198         }
199     }
200 }