001: package org.apache.turbine.modules;
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.List;
023:
024: import org.apache.commons.logging.Log;
025: import org.apache.commons.logging.LogFactory;
026:
027: import org.apache.turbine.Turbine;
028: import org.apache.turbine.TurbineConstants;
029: import org.apache.turbine.services.assemblerbroker.AssemblerBrokerService;
030: import org.apache.turbine.services.assemblerbroker.TurbineAssemblerBroker;
031: import org.apache.turbine.util.ObjectUtils;
032: import org.apache.turbine.util.RunData;
033:
034: /**
035: * The purpose of this class is to allow one to load and execute
036: * Action modules.
037: *
038: * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
039: * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
040: * @version $Id: ActionLoader.java 534527 2007-05-02 16:10:59Z tv $
041: */
042: public class ActionLoader extends GenericLoader {
043: /** Serial Version UID */
044: private static final long serialVersionUID = -2285549057406921958L;
045:
046: /** Logging */
047: private static Log log = LogFactory.getLog(ActionLoader.class);
048:
049: /** The single instance of this class. */
050: private static ActionLoader instance = new ActionLoader(Turbine
051: .getConfiguration().getInt(
052: TurbineConstants.ACTION_CACHE_SIZE_KEY,
053: TurbineConstants.ACTION_CACHE_SIZE_DEFAULT));
054:
055: /** The Assembler Broker Service */
056: private static AssemblerBrokerService ab = TurbineAssemblerBroker
057: .getService();
058:
059: /**
060: * These ctor's are private to force clients to use getInstance()
061: * to access this class.
062: */
063: private ActionLoader() {
064: super ();
065: }
066:
067: /**
068: * These ctor's are private to force clients to use getInstance()
069: * to access this class.
070: */
071: private ActionLoader(int i) {
072: super (i);
073: }
074:
075: /**
076: * Adds an instance of an object into the hashtable.
077: *
078: * @param name Name of object.
079: * @param action Action to be associated with name.
080: */
081: private void addInstance(String name, Action action) {
082: if (cache()) {
083: this .put(name, (Action) action);
084: }
085: }
086:
087: /**
088: * Attempts to load and execute the external action.
089: *
090: * @param data Turbine information.
091: * @param name Name of object that will execute the action.
092: * @exception Exception a generic exception.
093: */
094: public void exec(RunData data, String name) throws Exception {
095: // Execute action
096: getInstance(name).perform(data);
097: }
098:
099: /**
100: * Pulls out an instance of the object by name. Name is just the
101: * single name of the object.
102: *
103: * @param name Name of object instance.
104: * @return An Action with the specified name, or null.
105: * @exception Exception a generic exception.
106: */
107: public Action getInstance(String name) throws Exception {
108: Action action = null;
109:
110: // Check if the action is already in the cache
111: if (cache() && this .containsKey(name)) {
112: action = (Action) this .get(name);
113: log.debug("Found Action " + name + " in the cache!");
114: } else {
115: log.debug("Loading Action " + name
116: + " from the Assembler Broker");
117:
118: try {
119: // Attempt to load the screen
120: action = (Action) ab.getAssembler(
121: AssemblerBrokerService.ACTION_TYPE, name);
122: } catch (ClassCastException cce) {
123: // This can alternatively let this exception be thrown
124: // So that the ClassCastException is shown in the
125: // browser window. Like this it shows "Screen not Found"
126: action = null;
127: }
128:
129: if (action == null) {
130: // If we did not find a screen we should try and give
131: // the user a reason for that...
132: // FIX ME: The AssemblerFactories should each add it's
133: // own string here...
134: List packages = Turbine.getConfiguration().getList(
135: TurbineConstants.MODULE_PACKAGES);
136:
137: ObjectUtils.addOnce(packages, GenericLoader
138: .getBasePackage());
139:
140: throw new ClassNotFoundException(
141: "\n\n\tRequested Action not found: "
142: + name
143: + "\n\tTurbine looked in the following "
144: + "modules.packages path: \n\t"
145: + packages.toString() + "\n");
146: } else if (cache()) {
147: // The new instance is added to the cache
148: addInstance(name, action);
149: }
150: }
151: return action;
152: }
153:
154: /**
155: * The method through which this class is accessed.
156: *
157: * @return The single instance of this class.
158: */
159: public static ActionLoader getInstance() {
160: return instance;
161: }
162: }
|