001: /********************************************************************************
002: * CruiseControl, a Continuous Integration Toolkit
003: * Copyright (c) 2005 ThoughtWorks, Inc.
004: * 200 E. Randolph, 25th Floor
005: * Chicago, IL 60601 USA
006: * All rights reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: *
012: * + Redistributions of source code must retain the above copyright
013: * notice, this list of conditions and the following disclaimer.
014: *
015: * + Redistributions in binary form must reproduce the above
016: * copyright notice, this list of conditions and the following
017: * disclaimer in the documentation and/or other materials provided
018: * with the distribution.
019: *
020: * + Neither the name of ThoughtWorks, Inc., CruiseControl, nor the
021: * names of its contributors may be used to endorse or promote
022: * products derived from this software without specific prior
023: * written permission.
024: *
025: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
026: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
027: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
028: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
029: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
030: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
031: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
032: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
033: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
034: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
035: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
036: ********************************************************************************/package net.sourceforge.cruisecontrol.interceptor;
037:
038: import java.io.IOException;
039: import java.util.Map;
040:
041: import javax.management.AttributeNotFoundException;
042: import javax.management.InstanceNotFoundException;
043: import javax.management.MBeanException;
044: import javax.management.ReflectionException;
045:
046: import org.jdom.JDOMException;
047:
048: import net.sourceforge.cruisecontrol.Configuration;
049: import net.sourceforge.cruisecontrol.CruiseControlException;
050: import net.sourceforge.cruisecontrol.PluginDetail;
051:
052: import com.opensymphony.xwork.Action;
053: import com.opensymphony.xwork.ActionContext;
054: import com.opensymphony.xwork.ActionInvocation;
055: import com.opensymphony.xwork.interceptor.AroundInterceptor;
056:
057: /**
058: * Understands how to load available plugins for PluginAware actions.
059: */
060: public class PluginsInterceptor extends AroundInterceptor {
061: protected void before(ActionInvocation invocation) throws Exception {
062: Action action = invocation.getAction();
063:
064: if (action instanceof PluginsAware) {
065: ActionContext invocationContext = invocation
066: .getInvocationContext();
067: Map parameters = invocationContext.getParameters();
068:
069: String pluginType = ((String[]) parameters
070: .get("pluginType"))[0];
071: if (pluginType != null) {
072: PluginsAware pluginsAction = (PluginsAware) action;
073: Configuration configuration = getConfiguration(invocationContext);
074: PluginLocator locator = new PluginLocator(configuration);
075:
076: pluginsAction.setAvailablePlugins(getAvailablePlugins(
077: locator, pluginType));
078: Map session = invocation.getInvocationContext()
079: .getSession();
080: pluginsAction
081: .setConfiguredPlugins(getConfiguredPlugins(
082: locator, pluginType, (String) session
083: .get("project")));
084: }
085: }
086: }
087:
088: protected void after(ActionInvocation dispatcher, String result)
089: throws Exception {
090: }
091:
092: private PluginDetail[] getAvailablePlugins(PluginLocator locator,
093: String pluginType) throws AttributeNotFoundException,
094: InstanceNotFoundException, MBeanException,
095: ReflectionException, IOException {
096: return locator.getAvailablePlugins(pluginType);
097: }
098:
099: private PluginDetail[] getConfiguredPlugins(PluginLocator locator,
100: String pluginType, String project)
101: throws AttributeNotFoundException,
102: InstanceNotFoundException, MBeanException,
103: ReflectionException, IOException, CruiseControlException,
104: JDOMException {
105: if (project != null) {
106: return locator.getConfiguredPlugins(project, pluginType);
107: }
108:
109: return null;
110: }
111:
112: private Configuration getConfiguration(
113: ActionContext invocationContext) {
114: return (Configuration) invocationContext.getSession().get(
115: "cc-configuration");
116: }
117: }
|