001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 2003-2005 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: ApplyCatalinaAccessLoggerAction.java 9680 2006-10-06 12:08:33Z danesa $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas.webapp.jonasadmin.logging;
025:
026: import java.io.IOException;
027:
028: import javax.management.ObjectName;
029: import javax.servlet.ServletException;
030: import javax.servlet.http.HttpServletRequest;
031: import javax.servlet.http.HttpServletResponse;
032:
033: import org.apache.struts.action.ActionMessage;
034: import org.apache.struts.action.ActionForm;
035: import org.apache.struts.action.ActionForward;
036: import org.apache.struts.action.ActionMapping;
037: import org.objectweb.jonas.jmx.JonasManagementRepr;
038: import org.objectweb.jonas.jmx.CatalinaObjectName;
039: import org.objectweb.jonas.webapp.jonasadmin.JonasAdminException;
040: import org.objectweb.jonas.webapp.jonasadmin.WhereAreYou;
041:
042: /**
043: * @author Michel-Ange ANTON
044: * @author Adriana Danes
045: */
046: public class ApplyCatalinaAccessLoggerAction extends BaseLoggerAction {
047:
048: /**
049: * Signature for the <code>createStandardConnector</code> operation.
050: */
051: private String sa_CreateAccessLogger[] = { "java.lang.String" };
052:
053: private static String sDefaultForward = "ActionEditWebAppCatalina";
054:
055: // --------------------------------------------------------- Protected Variables
056:
057: // --------------------------------------------------------- Public Methods
058: /**
059: * Execute CatalinaAccessLogger action
060: * @param pForm The current form
061: * @param pMapping The current mapping
062: * @param pRequest The current request
063: * @param pResponse The response to the current request
064: * @return The forward to go to the next page
065: * @throws Exception
066: */
067: public ActionForward executeAction(ActionMapping pMapping,
068: ActionForm pForm, HttpServletRequest pRequest,
069: HttpServletResponse pResponse) throws IOException,
070: ServletException {
071:
072: // Default forward
073: ActionForward oForward = null;
074:
075: // Form used
076: CatalinaAccessLogValveForm oForm = (CatalinaAccessLogValveForm) pForm;
077:
078: // Populate
079: try {
080: if ("create".equals(oForm.getAction())) {
081: oForward = createAccessLogger(oForm, pMapping, pRequest);
082: } else if ("edit".equals(oForm.getAction())) {
083: // Save in memory the new datas
084: oForward = populateMbean(oForm, pMapping
085: .findForward("ActionEditCatalinaAccessLogger"),
086: pMapping, pRequest);
087: }
088: } catch (JonasAdminException e) {
089: // Build error
090: m_Errors.add("logger.catalina.access", new ActionMessage(e
091: .getId()));
092: saveErrors(pRequest, m_Errors);
093: // Return to the current page
094: oForward = new ActionForward(pMapping.getInput());
095: } catch (Throwable t) {
096: addGlobalError(t);
097: saveErrors(pRequest, m_Errors);
098: oForward = pMapping.findForward("Global Error");
099: }
100:
101: // Next Forward
102: return oForward;
103: }
104:
105: /**
106: * Create a new access logger.
107: * @param pForm The current form
108: * @param pMapping The current mapping
109: * @param pRequest The current request
110: * @return The forward to go to the next page
111: * @throws Exception
112: */
113: protected ActionForward createAccessLogger(
114: CatalinaAccessLogValveForm pForm, ActionMapping pMapping,
115: HttpServletRequest pRequest) throws Exception {
116: Object values[] = null;
117: // Look up the Catalina MBeanFactory
118: ObjectName onFactory = CatalinaObjectName.catalinaFactory();
119: // Create a new Access Logger Valve object
120: values = new Object[1];
121: String domainName = m_WhereAreYou
122: .getCurrentCatalinaDomainName();
123: // Determine container type
124: String containerType = pForm.getContainerType();
125: if (containerType.equals(m_Resources
126: .getMessage("label.loggers.container.engine"))) {
127: ObjectName onEngine = CatalinaObjectName
128: .catalinaEngine(domainName);
129: values[0] = (onEngine).toString();
130: } else if (containerType.equals(m_Resources
131: .getMessage("label.loggers.container.host"))) {
132: ObjectName onHost = CatalinaObjectName.catalinaHost(
133: domainName, pForm.getContainerName());
134: values[0] = (onHost).toString();
135: }
136: WhereAreYou oWhere = (WhereAreYou) pRequest.getSession()
137: .getAttribute(WhereAreYou.SESSION_NAME);
138: String jonasServerName = oWhere.getCurrentJonasServerName();
139: Object onValve = JonasManagementRepr.invoke(onFactory,
140: "createAccessLoggerValve", values,
141: sa_CreateAccessLogger, jonasServerName);
142: pForm.setObjectName((String) onValve);
143: // Populate
144: ActionForward oForward = populateMbean(pForm, pMapping
145: .findForward("ActionListLoggers"), pMapping, pRequest);
146:
147: // refresh tree
148: refreshTree(pRequest);
149: // Force the node selected in tree
150: m_WhereAreYou.selectNameNode(getTreeBranchName(DEPTH_SERVER)
151: + WhereAreYou.NODE_SEPARATOR + "logging"
152: + WhereAreYou.NODE_SEPARATOR
153: + LoggerItem.LOGGER_CATALINA_ACCESS_HOST, true);
154:
155: // Return the next forward
156: return oForward;
157: }
158:
159: /**
160: * Populate the Mbean Access Logger.
161: * @param p_Form The current form
162: * @param p_Forward The current forward
163: * @param p_Mapping The current mapping
164: * @param p_Request The current request
165: * @return If 'save' is requested then return the new forward to save
166: * else return the current forward
167: * @throws Exception Failed to populate Mbean
168: */
169: protected ActionForward populateMbean(
170: CatalinaAccessLogValveForm p_Form, ActionForward p_Forward,
171: ActionMapping p_Mapping, HttpServletRequest p_Request)
172: throws Exception {
173: ActionForward oForward = p_Forward;
174: // Access logger
175: ObjectName on = new ObjectName(p_Form.getObjectName());
176: setStringAttribute(on, "directory", p_Form.getDirectory());
177: setStringAttribute(on, "prefix", p_Form.getPrefix());
178: setStringAttribute(on, "suffix", p_Form.getSuffix());
179: setBooleanAttribute(on, "resolveHosts", p_Form.isResolveHosts());
180: setBooleanAttribute(on, "rotatable", p_Form.isRotatable());
181: setStringAttribute(on, "pattern", p_Form.getPattern());
182: // Save in configuration file
183: if (p_Form.isSave()) {
184: //ObjectName onServer = CatalinaObjectName.catalinaServer();
185: //JonasManagementRepr.invoke(onServer, "store", null, null);
186: p_Form.setSave(false);
187: p_Request.setAttribute("forward", p_Forward.getName());
188: oForward = p_Mapping.findForward("ActionEditServletServer");
189: }
190: return oForward;
191: }
192:
193: }
|