001: package org.apache.turbine.services.assemblerbroker;
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.util.HashMap;
023: import java.util.Iterator;
024: import java.util.List;
025: import java.util.Map;
026: import java.util.Vector;
027:
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030:
031: import org.apache.turbine.modules.Assembler;
032: import org.apache.turbine.services.InitializationException;
033: import org.apache.turbine.services.TurbineBaseService;
034: import org.apache.turbine.services.assemblerbroker.util.AssemblerFactory;
035: import org.apache.turbine.util.TurbineException;
036:
037: /**
038: * TurbineAssemblerBrokerService allows assemblers (like screens,
039: * actions and layouts) to be loaded from one or more AssemblerFactory
040: * classes. AssemblerFactory classes are registered with this broker
041: * by adding them to the TurbineResources.properties file.
042: *
043: * @author <a href="mailto:leon@opticode.co.za">Leon Messerschmidt</a>
044: * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
045: * @version $Id: TurbineAssemblerBrokerService.java 534527 2007-05-02 16:10:59Z tv $
046: */
047: public class TurbineAssemblerBrokerService extends TurbineBaseService
048: implements AssemblerBrokerService {
049: /** Logging */
050: private static Log log = LogFactory
051: .getLog(TurbineAssemblerBrokerService.class);
052:
053: /** A structure that holds the registered AssemblerFactories */
054: private Map factories = null;
055:
056: /**
057: * Get a list of AssemblerFactories of a certain type
058: *
059: * @param type type of Assembler
060: * @return list of AssemblerFactories
061: */
062: private List getFactoryGroup(String type) {
063: if (!factories.containsKey(type)) {
064: factories.put(type, new Vector());
065: }
066: return (List) factories.get(type);
067: }
068:
069: /**
070: * Utiltiy method to register all factories for a given type.
071: *
072: * @param type type of Assembler
073: * @throws TurbineException
074: */
075: private void registerFactories(String type) throws TurbineException {
076: List names = getConfiguration().getList(type);
077:
078: log.info("Registering " + names.size() + " " + type
079: + " factories.");
080:
081: for (Iterator it = names.iterator(); it.hasNext();) {
082: String factory = (String) it.next();
083: try {
084: Object o = Class.forName(factory).newInstance();
085: registerFactory(type, (AssemblerFactory) o);
086: }
087: // these must be passed to the VM
088: catch (ThreadDeath e) {
089: throw e;
090: } catch (OutOfMemoryError e) {
091: throw e;
092: }
093: // when using Class.forName(), NoClassDefFoundErrors are likely
094: // to happen (missing jar files)
095: catch (Throwable t) {
096: throw new TurbineException("Failed registering " + type
097: + " factory: " + factory, t);
098: }
099: }
100: }
101:
102: /**
103: * Initializes the AssemblerBroker and loads the AssemblerFactory
104: * classes registered in TurbineResources.Properties.
105: *
106: * @throws InitializationException
107: */
108: public void init() throws InitializationException {
109: factories = new HashMap();
110: try {
111: registerFactories(AssemblerBrokerService.ACTION_TYPE);
112: registerFactories(AssemblerBrokerService.SCREEN_TYPE);
113: registerFactories(AssemblerBrokerService.NAVIGATION_TYPE);
114: registerFactories(AssemblerBrokerService.LAYOUT_TYPE);
115: registerFactories(AssemblerBrokerService.PAGE_TYPE);
116: registerFactories(AssemblerBrokerService.SCHEDULEDJOB_TYPE);
117: } catch (TurbineException e) {
118: throw new InitializationException(
119: "AssemblerBrokerService failed to initialize", e);
120: }
121: setInit(true);
122: }
123:
124: /**
125: * Register a new AssemblerFactory under a certain type
126: *
127: * @param type type of Assembler
128: * @param factory factory to register
129: */
130: public void registerFactory(String type, AssemblerFactory factory) {
131: getFactoryGroup(type).add(factory);
132: }
133:
134: /**
135: * Attempt to retrieve an Assembler of a given type with
136: * a name. Cycle through all the registered AssemblerFactory
137: * classes of type and return the first non-null assembly
138: * found. If an assembly was not found return null.
139: *
140: * @param type type of Assembler
141: * @param name name of the requested Assembler
142: * @return an Assembler or null
143: * @throws TurbineException
144: */
145: public Assembler getAssembler(String type, String name)
146: throws TurbineException {
147: List facs = getFactoryGroup(type);
148:
149: Assembler assembler = null;
150: for (Iterator it = facs.iterator(); (assembler == null)
151: && it.hasNext();) {
152: AssemblerFactory fac = (AssemblerFactory) it.next();
153: try {
154: assembler = fac.getAssembler(name);
155: } catch (Exception e) {
156: throw new TurbineException(
157: "Failed to load an assembler for " + name
158: + " from the " + type + " factory "
159: + fac.getClass().getName(), e);
160: }
161: }
162: return assembler;
163: }
164: }
|