01: /*
02: * Copyright 2006-2007 Luca Garulli (luca.garulli@assetdata.it)
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package org.romaframework.wizard;
17:
18: import java.io.File;
19:
20: import org.apache.commons.logging.Log;
21: import org.apache.commons.logging.LogFactory;
22: import org.romaframework.core.flow.Controller;
23: import org.romaframework.core.resource.ResourceResolverListener;
24: import org.romaframework.wizard.command.WizardCommand;
25: import org.romaframework.wizard.command.WizardCommandManager;
26:
27: /**
28: * Resolver of Wizard Command implementations. It discover any available implementation in the classpath. For convention any Wizard
29: * Command implementation class must contain the 'Wizard" inside its name. Example: ShowStatisticsWizard.
30: *
31: * @author Luca Garulli (luca.garulli@assetdata.it)
32: */
33: public class WizardResourceResolver implements ResourceResolverListener {
34:
35: private static final String WIZARD_NAME = "Wizard";
36: private static final String CLASS_EXT = ".class";
37:
38: private static Log log = LogFactory
39: .getLog(WizardResourceResolver.class);
40:
41: public WizardResourceResolver() {
42: Controller.getInstance().registerListener(
43: ResourceResolverListener.class, this );
44: }
45:
46: public void addResource(File iFile, String name,
47: String packagePrefix, String startingPackage) {
48: if (!packagePrefix.startsWith(startingPackage))
49: return;
50:
51: if (!name.endsWith(CLASS_EXT))
52: return;
53:
54: if (!name.contains(WIZARD_NAME))
55: return;
56:
57: name = name.substring(0, name.length() - CLASS_EXT.length());
58:
59: try {
60: Class<?> cls = Class.forName(packagePrefix + name);
61: if (WizardCommand.class.isAssignableFrom(cls)) {
62: WizardCommand wiz = (WizardCommand) cls.newInstance();
63: WizardCommandManager.getInstance().addConfiguration(
64: wiz.getName(), wiz);
65: if (log.isDebugEnabled())
66: log
67: .debug("[WizardResourceResolver.addResource] Loading wizard '"
68: + wiz.getName() + "' -> " + cls);
69: }
70: } catch (Exception e) {
71: }
72: }
73: }
|