001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 2006 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: ApplyCreateResourceAdapterAction.java 9680 2006-10-06 12:08:33Z danesa $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas.webapp.jonasadmin.resourceadapter;
025:
026: import java.io.File;
027: import java.io.IOException;
028: import java.io.StringWriter;
029:
030: import javax.management.ObjectName;
031: import javax.servlet.ServletException;
032: import javax.servlet.http.HttpServletRequest;
033: import javax.servlet.http.HttpServletResponse;
034:
035: import org.apache.struts.action.ActionForm;
036: import org.apache.struts.action.ActionForward;
037: import org.apache.struts.action.ActionMapping;
038: import org.apache.struts.action.ActionMessage;
039: import org.apache.velocity.VelocityContext;
040: import org.apache.velocity.app.VelocityEngine;
041: import org.objectweb.jonas.common.JProp;
042: import org.objectweb.jonas.jmx.JonasManagementRepr;
043: import org.objectweb.jonas.webapp.jonasadmin.JonasAdminJmx;
044: import org.objectweb.jonas.webapp.jonasadmin.WhereAreYou;
045: import org.objectweb.jonas.webapp.jonasadmin.deploy.BaseDeployAction;
046: import org.objectweb.jonas.webapp.jonasadmin.xml.ArchiveConfigForm;
047:
048: /**
049: * The action used when a Resource Adapter is created.
050: * This uses a Velocity template to build up the XML and creates a RAR file.
051: *
052: * @author Patrick Smith
053: */
054: public class ApplyCreateResourceAdapterAction extends BaseDeployAction {
055:
056: /**
057: * The location of JOnAS' base directory.
058: */
059: private String jonasBase = JProp.getJonasBase();
060:
061: /**
062: * The file separator to use for file access.
063: */
064: private String fileSeparator = File.separator;
065:
066: /**
067: * The name of the directory that stores RARs.
068: */
069: private String rarDir = "rars";
070:
071: /**
072: * The extention the new files should use.
073: */
074: private String extention = ".rar";
075:
076: // --------------------------------------------------------- Public Methods
077:
078: /**
079: * The action to use when the Struts action is executed.
080: * Create the appropriate RAR and forward to the RAR configuration tool
081: * if no errors exist.
082: * @param p_Mapping The ActionMapping for the action.
083: * @param p_Form The form used in this action.
084: * @param p_Request HTTP Request for the action.
085: * @param p_Response The HTTP Response.
086: * @throws IOException, ServletException if the there is a problem with the
087: * template files, creation of the RARs, or a servlet error.
088: * @return A forward to the next Struts page.
089: */
090: public ActionForward executeAction(ActionMapping p_Mapping,
091: ActionForm p_Form, HttpServletRequest p_Request,
092: HttpServletResponse p_Response) throws IOException,
093: ServletException {
094:
095: WhereAreYou oWhere = (WhereAreYou) p_Request.getSession()
096: .getAttribute(WhereAreYou.SESSION_NAME);
097: String serverName = oWhere.getCurrentJonasServerName();
098:
099: // Form used
100: CreateResourceAdapterForm oForm = (CreateResourceAdapterForm) p_Form;
101:
102: // The path within the new RAR to edit in the xml editor.
103: String path;
104:
105: // Create a VelocityEngine and set the properties of where to find
106: // the templates for the specific rar files.
107: VelocityEngine ve = new VelocityEngine();
108: ve.setProperty(VelocityEngine.VM_LIBRARY, "");
109: ve.setProperty(VelocityEngine.RESOURCE_LOADER, "file");
110: ve.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH, JProp
111: .getInstallRoot()
112: + fileSeparator
113: + "templates"
114: + fileSeparator
115: + "resourceadapter");
116:
117: try {
118: if (oForm.getTemplate().equals("Other")) {
119: path = "META-INF/ra.xml";
120: createRa(oForm, ve, serverName);
121: } else {
122: path = "META-INF/jonas-ra.xml";
123: createJonasRa(oForm, ve, serverName);
124: }
125: } catch (Exception e) {
126: m_Errors.add("error.resourceadapter.load.fail",
127: new ActionMessage(
128: "error.resourceadapter.load.fail", oForm
129: .getRarName()));
130: saveErrors(p_Request, m_Errors);
131: if (oForm.getTemplate().equals("Other")) {
132: return (p_Mapping
133: .findForward("Create Other Resource Adapter"));
134: } else {
135: return (p_Mapping
136: .findForward("Create JDBC Resource Adapter"));
137: }
138: }
139:
140: m_Session.removeAttribute("createResourceAdapterForm");
141:
142: ArchiveConfigForm form = new ArchiveConfigForm();
143: form.reset();
144:
145: // set the list of deployable files, these are the only files that
146: // can be configured
147: form.setDeployable(JonasAdminJmx
148: .getRarFilesDeployable(serverName));
149: form.setIsDomain(isDomain());
150: form.setArchiveName(oForm.getRarName() + extention);
151: form.setPathName(path);
152: m_Session.setAttribute("archiveConfigForm", form);
153: p_Request.setAttribute("file", path);
154: return (p_Mapping.findForward("ActionArchiveConfig"));
155: }
156:
157: // --------------------------------------------------------- Protected Methods
158:
159: /**
160: * Creates an empty jonas-ra.xml content header.
161: * @return The content of an empty jonas-ra XML file.
162: */
163: protected String createEmptyJonasRa() {
164: StringBuffer sb = new StringBuffer();
165: sb.append("<?xml version=\"1.0\"?>\n");
166: sb
167: .append("<jonas-connector xmlns=\"http://www.objectweb.org/jonas/ns\" ");
168: sb
169: .append("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" ");
170: sb
171: .append("xsi:schemaLocation=\"http://www.objectweb.org/jonas/ns ");
172: sb
173: .append("http://www.objectweb.org/jonas/ns/jonas-connector_4_4.xsd\"/>");
174: return sb.toString();
175: }
176:
177: /**
178: * Create a ra.xml file (from a Velocity template) and adds it to a RAR file.
179: * @param oForm The struts form used in this Struts action.
180: * @param ve The Velocity engine that contains the appropriate template.
181: * @throws Exception If the invokation of creating the RAR or adding a file to the RAR fails.
182: */
183: protected void createRa(CreateResourceAdapterForm oForm,
184: VelocityEngine ve, String serverName) throws Exception {
185: ve.init();
186: VelocityContext context = new VelocityContext();
187: context.put("oForm", oForm);
188: StringWriter st = new StringWriter();
189: ve.mergeTemplate("ra-xml.vm", context, st);
190: String raXml = st.toString();
191:
192: ObjectName on = JonasAdminJmx
193: .getArchiveConfigObjectName(serverName);
194:
195: String fileName = jonasBase + fileSeparator + rarDir
196: + fileSeparator + oForm.getRarName() + extention;
197:
198: Object[] params = new Object[] { fileName, "META-INF/ra.xml",
199: raXml };
200:
201: String[] sig = new String[] { "java.lang.String",
202: "java.lang.String", "java.lang.String" };
203:
204: JonasManagementRepr.invoke(on, "createArchiveWithXmlFile",
205: params, sig, serverName);
206:
207: params = new Object[] { fileName, "META-INF/jonas-ra.xml",
208: createEmptyJonasRa() };
209: JonasManagementRepr.invoke(on, "addXML", params, sig,
210: serverName);
211: }
212:
213: /**
214: * Create a ra.xml file (from a Velocity template) and adds it to a RAR file.
215: * @param oForm The struts form used in this Struts action.
216: * @param ve The Velocity engine that contains the appropriate template.
217: * @throws Exception If the invokation of creating the RAR or adding a file to the RAR fails.
218: */
219: protected void createJonasRa(CreateResourceAdapterForm oForm,
220: VelocityEngine ve, String serverName) throws Exception {
221: ve.init();
222: VelocityContext context = new VelocityContext();
223: context.put("oForm", oForm);
224: StringWriter st = new StringWriter();
225: ve.mergeTemplate("jonas-ra-xml.vm", context, st);
226: String raXml = st.toString();
227: ObjectName on = JonasAdminJmx
228: .getArchiveConfigObjectName(serverName);
229:
230: String fileName = jonasBase + fileSeparator + rarDir
231: + fileSeparator + oForm.getRarName() + extention;
232: Object[] params = new Object[] { fileName,
233: "META-INF/jonas-ra.xml", raXml };
234:
235: String[] sig = new String[] { "java.lang.String",
236: "java.lang.String", "java.lang.String" };
237:
238: JonasManagementRepr.invoke(on, "createArchiveWithXmlFile",
239: params, sig, serverName);
240: }
241: }
|