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: CatalinaAccessLogValveForm.java 9864 2006-11-24 10:14:22Z danesa $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas.webapp.jonasadmin.logging;
025:
026: import java.util.List;
027:
028: import javax.management.MalformedObjectNameException;
029: import javax.management.ObjectName;
030: import javax.servlet.http.HttpServletRequest;
031:
032: import org.apache.struts.action.ActionMessage;
033: import org.apache.struts.action.ActionErrors;
034: import org.apache.struts.action.ActionMapping;
035: import org.objectweb.jonas.jmx.CatalinaObjectName;
036: import org.objectweb.jonas.jmx.JonasManagementRepr;
037: import org.objectweb.jonas.webapp.jonasadmin.Jlists;
038: import org.objectweb.jonas.webapp.jonasadmin.WhereAreYou;
039:
040: /**
041: * @author Michel-Ange ANTON
042: * @author Adriana Danes
043: */
044:
045: public final class CatalinaAccessLogValveForm extends CatalinaValveForm {
046:
047: // ----------------------------------------------------- Properties Variables
048:
049: /**
050: * directory in which log files will be placed
051: */
052: private String directory = null;
053: /**
054: * A formatting layout
055: */
056: private String pattern = null;
057: /**
058: * prefix added to the start of each log file's name
059: */
060: private String prefix = null;
061: /**
062: * suffix added to the end of each log file's name
063: */
064: private String suffix = null;
065: /**
066: * Deafult true. Flag to determine if log rotation should occure.
067: */
068: private boolean rotatable = true;
069: /**
070: * Set to true to convert the IP address of the remote host into the corresponding host name via a DNS lookup.
071: * Set to false to skip this lookup, and report the remote IP address instead.
072: */
073: private boolean resolveHosts = false;
074: /**
075: *
076: */
077: private List booleanValues = Jlists.getBooleanValues();
078: /**
079: * Domain name used to construct Catalina MBean ObjectNames (JOnAS server name in general)
080: */
081: private String catalinaDomainName = null;
082:
083: // --------------------------------------------------------- Public Methods
084:
085: public void reset(ActionMapping mapping, HttpServletRequest request) {
086: super .reset(mapping, request);
087: directory = "logs";
088: prefix = "access_log.";
089: suffix = ".txt";
090: pattern = "%h %l %u %t \"%r\" %s %b";
091: resolveHosts = false;
092: rotatable = true;
093: String[] containerTypes = { "Engine", "Host" };
094: setContainerTypes(containerTypes);
095: }
096:
097: public ActionErrors validate(ActionMapping mapping,
098: HttpServletRequest request) {
099:
100: WhereAreYou oWhere = (WhereAreYou) request.getSession()
101: .getAttribute(WhereAreYou.SESSION_NAME);
102: String serverName = oWhere.getCurrentJonasServerName();
103:
104: ActionErrors oErrors = new ActionErrors();
105: // prefix
106: if ((prefix == null) || (prefix.length() < 1)) {
107: oErrors.add("pattern", new ActionMessage(
108: "error.logger.catalina.access.prefix.required"));
109: }
110: // default is a 0 length string
111: if ((suffix == null) || (suffix.length() < 1)) {
112: suffix = "";
113: }
114: // pattern
115: if ((pattern == null) || (pattern.length() < 1)) {
116: oErrors.add("pattern", new ActionMessage(
117: "error.logger.catalina.access.pattern.required"));
118: }
119: // Test the provided Engine or Host names only in case we are in a "create" action
120: if (getAction().equals("edit")) {
121: return oErrors;
122: }
123: if (getContainerType().equals("Engine")) {
124: // Test if no access valve set already on the Engine
125: ObjectName onEngine = null;
126: try {
127: onEngine = CatalinaObjectName
128: .catalinaEngine(catalinaDomainName);
129: } catch (MalformedObjectNameException e) {
130: oErrors.add("", new ActionMessage(e.toString()));
131: return oErrors;
132: }
133: ObjectName[] valveOns = (ObjectName[]) JonasManagementRepr
134: .getAttribute(onEngine, "valveObjectNames",
135: serverName);
136: for (int i = 0; i < valveOns.length; i++) {
137: ObjectName valveOn = valveOns[i];
138: if (valveOn != null) { // can be null because bug in Tomacat (after a remove)
139: String valveName = valveOn.getKeyProperty("name");
140: if (valveName.equals("AccessLogValve")) {
141: // we already have an AccessLogValve associated to the Engine
142: oErrors
143: .add(
144: "",
145: new ActionMessage(
146: "error.logger.catalina.access.log.engine.alreadypresent"));
147: break;
148: }
149: }
150: }
151: }
152: if (getContainerType().equals("Host")) {
153: // Test if no access valve set already on the choosen host
154: ObjectName onHost;
155: try {
156: onHost = CatalinaObjectName.catalinaHost(
157: catalinaDomainName, getContainerName());
158: } catch (MalformedObjectNameException e) {
159: oErrors.add("", new ActionMessage(e.toString()));
160: return oErrors;
161: }
162: if (!JonasManagementRepr.isRegistered(onHost, serverName)) {
163: oErrors.add("", new ActionMessage(
164: "error.logger.catalina.access.log.nohost"));
165: } else {
166: ObjectName[] valveOns = (ObjectName[]) JonasManagementRepr
167: .getAttribute(onHost, "valveObjectNames",
168: serverName);
169: for (int i = 0; i < valveOns.length; i++) {
170: ObjectName valveOn = valveOns[i];
171: if (valveOn != null) { // can be null because bug in Tomacat (after a remove)
172: String valveName = valveOn
173: .getKeyProperty("name");
174: if (valveName.equals("AccessLogValve")) {
175: // we already have an AccessLogValve associated to the Host
176: oErrors
177: .add(
178: "",
179: new ActionMessage(
180: "error.logger.catalina.access.log.host.alreadypresent"));
181: break;
182: }
183: }
184: }
185: }
186: }
187: return oErrors;
188: }
189:
190: // ------------------------------------------------------------- Properties Methods
191:
192: public List getBooleanValues() {
193: return booleanValues;
194: }
195:
196: public void setBooleanValues(List booleanValues) {
197: this .booleanValues = booleanValues;
198: }
199:
200: public String getDirectory() {
201: return this .directory;
202: }
203:
204: public void setDirectory(String directory) {
205: this .directory = directory;
206: }
207:
208: public String getPattern() {
209: return this .pattern;
210: }
211:
212: public void setPattern(String pattern) {
213: this .pattern = pattern;
214: }
215:
216: public String getPrefix() {
217: return this .prefix;
218: }
219:
220: public void setPrefix(String prefix) {
221: this .prefix = prefix;
222: }
223:
224: public String getSuffix() {
225: return this .suffix;
226: }
227:
228: public void setSuffix(String suffix) {
229: this .suffix = suffix;
230: }
231:
232: public boolean isResolveHosts() {
233: return this .resolveHosts;
234: }
235:
236: public void setResolveHosts(boolean resolveHosts) {
237: this .resolveHosts = resolveHosts;
238: }
239:
240: public boolean isRotatable() {
241: return this .rotatable;
242: }
243:
244: public void setRotatable(boolean rotatable) {
245: this .rotatable = rotatable;
246: }
247:
248: /**
249: * @return Returns the catalinaDomainName.
250: */
251: public String getCatalinaDomainName() {
252: return catalinaDomainName;
253: }
254:
255: /**
256: * @param catalinaDomainName The catalinaDomainName to set.
257: */
258: public void setCatalinaDomainName(String catalinaDomainName) {
259: this.catalinaDomainName = catalinaDomainName;
260: }
261:
262: }
|