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.net.InetAddress;
040: import java.util.Map;
041:
042: import javax.management.MalformedObjectNameException;
043:
044: import net.sourceforge.cruisecontrol.Configuration;
045:
046: import com.opensymphony.xwork.Action;
047: import com.opensymphony.xwork.ActionInvocation;
048: import com.opensymphony.xwork.interceptor.AroundInterceptor;
049:
050: /**
051: * Understands how to load the configuration for ConfigurationAware actions.
052: */
053: public class ConfigurationInterceptor extends AroundInterceptor {
054: protected void before(ActionInvocation invocation) throws Exception {
055: Action action = invocation.getAction();
056:
057: Map parameters = invocation.getInvocationContext()
058: .getParameters();
059: if (parameters.get("project") != null) {
060: Map session = invocation.getInvocationContext()
061: .getSession();
062: session.put("project", ((String[]) parameters
063: .get("project"))[0]);
064: }
065:
066: Configuration configuration = getConfiguration(invocation);
067:
068: if (action instanceof ConfigurationAware) {
069: ((ConfigurationAware) action)
070: .setConfiguration(configuration);
071: }
072: }
073:
074: protected void after(ActionInvocation dispatcher, String result)
075: throws Exception {
076: }
077:
078: private Configuration createConfiguration() throws IOException,
079: MalformedObjectNameException {
080: int rmiPort = Integer.parseInt(System
081: .getProperty("cruisecontrol.rmiport"));
082: Configuration configuration = new Configuration(getJMXServer(),
083: rmiPort);
084: return configuration;
085: }
086:
087: private Configuration getConfiguration(ActionInvocation invocation)
088: throws IOException, MalformedObjectNameException {
089: Map session = invocation.getInvocationContext().getSession();
090: Configuration configuration = (Configuration) session
091: .get("cc-configuration");
092:
093: if (configuration == null) {
094: configuration = createConfiguration();
095: session.put("cc-configuration", configuration);
096: invocation.getInvocationContext().setSession(session);
097: }
098:
099: return configuration;
100: }
101:
102: private String getJMXServer() {
103: String jmxServer;
104: try {
105: jmxServer = InetAddress.getLocalHost().getHostName();
106: } catch (IOException e) {
107: jmxServer = "localhost";
108: }
109: return jmxServer;
110: }
111: }
|