View Javadoc

1   /**
2    * $Id: BaseAbstractResourceManager.java 77 2006-12-15 21:36:03Z maldito_orco $
3    * $Revision: 77 $
4    * $Date: 2006-12-15 18:36:03 -0300 (Fri, 15 Dec 2006) $
5    *
6    * =========================================================================
7    *
8    * Copyright 2005 Tubo
9    *
10   *  Licensed under the Apache License, Version 2.0 (the "License");
11   *  you may not use this file except in compliance with the License.
12   *  You may obtain a copy of the License at
13   *
14   *     http://www.apache.org/licenses/LICENSE-2.0
15   *
16   *  Unless required by applicable law or agreed to in writing, software
17   *  distributed under the License is distributed on an "AS IS" BASIS,
18   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19   *  See the License for the specific language governing permissions and
20   *  limitations under the License.
21   */
22  package org.tubo.resource.baseimpl;
23  
24  //log
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  //tubo
28  import org.tubo.resource.ResourceManager;
29  import org.tubo.resource.Manager;
30  import org.tubo.resource.flow.FlowExecutor;
31  import org.tubo.resource.component.ComponentManager;
32  import org.tubo.resource.consumer.ConsumerManager;
33  import org.tubo.configuration.def.Configuration;
34  import org.tubo.configuration.def.ConsumerDef;
35  import org.tubo.exception.TuboResourceException;
36  import org.tubo.exception.ExceptionManager;
37  import org.tubo.exception.TuboException;
38  //util
39  import java.util.List;
40  import java.util.Iterator;
41  import java.util.ArrayList;
42  import java.util.Map;
43  
44  /**
45   * Created: Aug 31, 2006, 6:47:45 PM
46   * Last Modification Date: $Date: 2006-12-15 18:36:03 -0300 (Fri, 15 Dec 2006) $
47   *
48   * @author maldito_orco (maldito_orco@users.sourceforge.net)
49   * @version $Revision: 77 $
50   */
51  public abstract class BaseAbstractResourceManager  implements ResourceManager {
52      public static final String RCS_ID = "$Id: BaseAbstractResourceManager.java 77 2006-12-15 21:36:03Z maldito_orco $";
53      private static Log log = LogFactory.getLog(BaseAbstractResourceManager.class);
54  
55      /** Tubo configuration */
56      private Configuration configuration = null;
57  
58      /**
59       * Default constructor protected. Used by test cases.
60       * Don't change. USED ONLY BY TEST CASES!
61       * @throws TuboResourceException
62       */
63      protected BaseAbstractResourceManager() throws TuboException {
64      }
65  
66      /*TODO: erase
67       * Constructor
68       * @param configuration
69       * @throws TuboResourceException
70       */
71      /*public BaseAbstractResourceManager(Configuration configuration) throws TuboException {
72          //
73          // call default constructor
74          //this();
75          //
76          // set configuration
77          this.configuration = configuration;
78          //
79          // call initialize resource manager implementation
80          initialize();
81          //
82          // setup
83          try {
84              setup();
85          } catch (TuboException e) {
86              //
87              // log error
88              log.error("Error while setup ResourceManager", e);
89              //
90              // finish on error
91              throw e;
92          }
93      }*/
94  
95      /*TODO: erase
96       * Hook method for inizialise Resource.
97       * @throws TuboResourceException
98       */
99      /*protected abstract void initialize() throws TuboException;*/
100 
101     /*TODO: erase
102      * Setup components
103      */
104     /*protected void setup() throws TuboException {
105         // setup singleton ExceptionManager
106         ExceptionManager exceptionManager = getExceptionManager();
107         exceptionManager.setResourceManager(this);
108         //
109         // setup FlowExecutor
110         FlowExecutor flowExecutor = getFlowExecutor();
111         flowExecutor.setResourceManager(this);
112         //
113         // setup all ConsumerManager
114         List consumerManagers = getConsumerManagers();
115         for(Iterator it=consumerManagers.iterator(); it.hasNext();) {
116             Manager manager = (Manager)it.next();
117             manager.setResourceManager(this);
118         }
119         //
120         // TODO: miss components, connections ...
121     }*/
122 
123     /**
124      * Get Configuration object of this resource manager
125      * @return Configuration
126      */
127     public Configuration getConfiguration() throws TuboException {
128         return configuration;
129     }
130 
131 
132     public void setConfiguration(Configuration configuration) {
133         this.configuration = configuration;
134     }
135 
136     /**
137      * Return consumer manager identified by id
138      * @param id ConsumerManager identificator
139      * @return A COnsumerManager
140      */
141     public ConsumerManager getConsumerManager(String id) throws TuboException {
142         //
143         // get manager identified by id
144         Manager manager = getManager(id);
145         //
146         // define var
147         ConsumerManager consumerManager;
148         //
149         // check instanceof
150         if (manager instanceof ConsumerManager) {
151             //
152             // cast manager to Consumermanager
153             consumerManager = (ConsumerManager)manager;
154         }
155         else
156             throw getExceptionManager().getException(1300,id);
157         //
158         // return ConsumerManager
159         return consumerManager;
160     }
161 
162     public List getConsumerManagers() throws TuboException {
163         //
164         // create return value
165         List managers = new ArrayList();
166         //
167         // get consumer definitions
168         Map defs = configuration.getConsumerDefs();
169         //
170         // iterate over all consumer definitions, extract id and get Manager for th
171         for (Iterator it=defs.values().iterator(); it.hasNext();) {
172             ConsumerDef def = (ConsumerDef)it.next();
173             ConsumerManager manager = getConsumerManager(def.getId());
174             managers.add(manager);
175         }
176         //
177         // return managers
178         return managers;
179     }
180 
181     /**
182      * This method returns a ComponentManager identified by id
183      * @param id ConsumerManager identifier
184      * @return a ConsumerManager instance
185      */
186     public ComponentManager getComponentManager(String id) throws TuboException {
187         //
188         // get Manager identified by id
189         Manager manager = getManager(id);
190         //
191         // define var
192         ComponentManager componentManager;
193         //
194         // check instanceof
195         if (manager instanceof ComponentManager) {
196             //
197             // cast manager to ComponentManager
198             componentManager = (ComponentManager)manager;
199         }
200         else
201             throw getExceptionManager().getException(1301,id);
202         //
203         // return ComponentManager
204         return componentManager;
205     }
206 
207 }