001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.console.car;
017:
018: import java.io.BufferedReader;
019: import java.io.IOException;
020: import java.io.StringReader;
021: import java.util.ArrayList;
022: import java.util.List;
023:
024: import javax.portlet.ActionRequest;
025: import javax.portlet.ActionResponse;
026: import javax.portlet.PortletException;
027: import javax.portlet.RenderRequest;
028: import javax.portlet.RenderResponse;
029:
030: import org.apache.commons.logging.Log;
031: import org.apache.commons.logging.LogFactory;
032: import org.apache.geronimo.console.MultiPageModel;
033: import org.apache.geronimo.console.util.PortletManager;
034: import org.apache.geronimo.kernel.repository.Artifact;
035: import org.apache.geronimo.kernel.repository.Dependency;
036: import org.apache.geronimo.kernel.repository.ImportType;
037: import org.apache.geronimo.system.plugin.PluginInstallerGBean;
038: import org.apache.geronimo.system.plugin.PluginInstaller;
039: import org.apache.geronimo.system.plugin.model.ArtifactType;
040: import org.apache.geronimo.system.plugin.model.DependencyType;
041: import org.apache.geronimo.system.plugin.model.LicenseType;
042: import org.apache.geronimo.system.plugin.model.PluginArtifactType;
043: import org.apache.geronimo.system.plugin.model.PluginType;
044: import org.apache.geronimo.system.plugin.model.PrerequisiteType;
045:
046: /**
047: * Handler for the screen where you configure plugin data before exporting
048: *
049: * @version $Rev: 631628 $ $Date: 2008-02-27 08:12:49 -0800 (Wed, 27 Feb 2008) $
050: */
051: public class ExportConfigHandler extends BaseImportExportHandler {
052: private final static Log log = LogFactory
053: .getLog(ExportConfigHandler.class);
054:
055: public ExportConfigHandler() {
056: super (CONFIGURE_EXPORT_MODE,
057: "/WEB-INF/view/car/pluginParams.jsp");
058: }
059:
060: public String actionBeforeView(ActionRequest request,
061: ActionResponse response, MultiPageModel model)
062: throws PortletException, IOException {
063: String configId = request.getParameter("configId");
064: if (configId != null) {
065: response.setRenderParameter("configId", configId);
066: }
067: return getMode();
068: }
069:
070: public void renderView(RenderRequest request,
071: RenderResponse response, MultiPageModel model)
072: throws PortletException, IOException {
073: String configId = request.getParameter("configId");
074: PluginInstaller pluginInstaller = ManagementHelper
075: .getManagementHelper(request).getPluginInstaller();
076: Artifact newArtifact = Artifact.create(configId);
077: PluginType metadata = pluginInstaller
078: .getPluginMetadata(newArtifact);
079: PluginArtifactType instance = metadata.getPluginArtifact().get(
080: 0);
081: request.setAttribute("configId", PluginInstallerGBean
082: .toArtifact(instance.getModuleId()).toString());
083: request.setAttribute("name", metadata.getName());
084: request.setAttribute("repository", combine(instance
085: .getSourceRepository()));
086: request.setAttribute("category", metadata.getCategory());
087: request.setAttribute("url", metadata.getUrl());
088: request.setAttribute("author", metadata.getAuthor());
089: request.setAttribute("description", metadata.getDescription());
090: List<LicenseType> licenses = metadata.getLicense();
091: if (licenses != null && licenses.size() > 0) {
092: request.setAttribute("license", licenses.get(0).getValue());
093: if (licenses.get(0).isOsiApproved()) {
094: request.setAttribute("licenseOSI", "true");
095: }
096: if (licenses.size() > 1) {
097: log
098: .warn("Unable to edit plugin metadata containing more than one license! Additional license data will not be editable.");
099: }
100: }
101: //Choose the first geronimo-versions element and set the config version element to that version number.
102: List<String> gerVers = instance.getGeronimoVersion();
103: if (gerVers != null && gerVers.size() > 0) {
104: request.setAttribute("geronimoVersion", gerVers.get(0));
105: }
106: request.setAttribute("jvmVersions", combine(instance
107: .getJvmVersion()));
108: request.setAttribute("dependencies", toString(instance
109: .getDependency()));
110: request.setAttribute("obsoletes", toString(instance
111: .getObsoletes()));
112: List<PrerequisiteType> reqs = instance.getPrerequisite();
113: if (reqs != null && reqs.size() > 0) {
114: int i = 1;
115: for (PrerequisiteType prereq : reqs) {
116: String prefix = "prereq" + i;
117: request.setAttribute(prefix, PluginInstallerGBean
118: .toArtifact(prereq.getId()).toString());
119: request.setAttribute(prefix + "type", prereq
120: .getResourceType());
121: request.setAttribute(prefix + "desc", prereq
122: .getDescription());
123: }
124: if (reqs.size() > 3) {
125: log
126: .warn("Unable to edit plugin metadata containing more than three prerequisites! Additional prereqs will not be editable.");
127: }
128: }
129: }
130:
131: public String actionAfterView(ActionRequest request,
132: ActionResponse response, MultiPageModel model)
133: throws PortletException, IOException {
134: String configId = request.getParameter("configId");
135: PluginInstaller pluginInstaller = ManagementHelper
136: .getManagementHelper(request).getPluginInstaller();
137: PluginType metadata = pluginInstaller
138: .getPluginMetadata(Artifact.create(configId));
139: PluginArtifactType instance = metadata.getPluginArtifact().get(
140: 0);
141:
142: String name = request.getParameter("name");
143: metadata.setName(name);
144: metadata.setCategory(request.getParameter("category"));
145: metadata.setUrl(request.getParameter("url"));
146: metadata.setAuthor(request.getParameter("author"));
147: metadata.setDescription(request.getParameter("description"));
148: String licenseString = request.getParameter("license");
149: String osi = request.getParameter("licenseOSI");
150: List<LicenseType> licenses = metadata.getLicense();
151: if (!licenses.isEmpty()) {
152: licenses.remove(0);
153: }
154: if (licenseString != null && !licenseString.trim().equals("")) {
155: LicenseType license = new LicenseType();
156: license.setValue(licenseString.trim());
157: license.setOsiApproved(osi != null && !osi.equals(""));
158: licenses.add(0, license);
159: }
160:
161: String jvmsString = request.getParameter("jvmVersions");
162: split(jvmsString, instance.getJvmVersion());
163:
164: String deps = request.getParameter("dependencies");
165: toDependencies(split(deps), instance.getDependency());
166:
167: String obsoletes = request.getParameter("obsoletes");
168: toArtifacts(split(obsoletes), instance.getObsoletes());
169:
170: String repo = request.getParameter("repository");
171: split(repo, instance.getSourceRepository());
172:
173: //TODO this is wrong, we are only supplying one version to the UI
174: String version = request.getParameter("geronimoVersion");
175: split(version, instance.getGeronimoVersion());
176:
177: List<PrerequisiteType> prereqs = instance.getPrerequisite();
178: //TODO this is probably wrong if # of prereqs is changed.
179: for (int i = 0; i < 3 && !prereqs.isEmpty(); i++) {
180: prereqs.remove(0);
181: }
182: int counter = 1;
183: while (true) {
184: String prefix = "prereq" + counter;
185: ++counter;
186: String id = request.getParameter(prefix);
187: if (id == null || id.trim().equals("")) {
188: break;
189: }
190: String type = request.getParameter(prefix + "type");
191: String desc = request.getParameter(prefix + "desc");
192: if (type != null && type.trim().equals("")) {
193: type = null;
194: }
195: if (desc != null && desc.trim().equals("")) {
196: desc = null;
197: }
198: PrerequisiteType prereq = new PrerequisiteType();
199: prereq.setResourceType(type);
200: prereq.setDescription(desc);
201: prereq.setId(PluginInstallerGBean.toArtifactType(Artifact
202: .create(id)));
203: prereqs.add(counter - 1, prereq);
204: }
205:
206: // Save updated metadata
207: pluginInstaller.updatePluginMetadata(metadata);
208:
209: response.setRenderParameter("configId", configId);
210: response.setRenderParameter("name", name);
211:
212: return CONFIRM_EXPORT_MODE + BEFORE_ACTION;
213: }
214:
215: private List<String> split(String deps) {
216: List<String> split = new ArrayList<String>();
217: if (deps != null && !deps.equals("")) {
218: split(deps, split);
219: }
220: return split;
221: }
222:
223: private void split(String deps, List<String> split) {
224: split.clear();
225: BufferedReader in = new BufferedReader(new StringReader(deps));
226: String line;
227: try {
228: while ((line = in.readLine()) != null) {
229: line = line.trim();
230: if (!line.equals("")) {
231: split.add(line);
232: }
233: }
234: in.close();
235: } catch (IOException e) {
236: log.error("Unable to parse request arguments", e);
237: }
238: }
239:
240: private String combine(List<String> strings) {
241: if (strings == null || strings.size() == 0) {
242: return null;
243: }
244: StringBuffer buf = new StringBuffer();
245: boolean first = true;
246: for (String string : strings) {
247: if (!first) {
248: buf.append("\n");
249: }
250: first = false;
251: buf.append(string);
252: }
253: return buf.toString();
254: }
255:
256: private void toArtifacts(List<String> artifacts,
257: List<ArtifactType> result) {
258: result.clear();
259: for (String artifact : artifacts) {
260: result.add(PluginInstallerGBean.toArtifactType(Artifact
261: .create(artifact)));
262: }
263: }
264:
265: private void toDependencies(List<String> artifacts,
266: List<DependencyType> result) {
267: result.clear();
268: for (String artifact : artifacts) {
269: //TODO this is wrong.... need to encode import type as well
270: result.add(PluginInstallerGBean.toDependencyType(
271: new Dependency(Artifact.create(artifact),
272: ImportType.ALL), true));
273: }
274: }
275:
276: private String toString(List<? extends ArtifactType> artifacts) {
277: if (artifacts == null || artifacts.size() == 0) {
278: return null;
279: }
280: StringBuffer buf = new StringBuffer();
281: boolean first = true;
282: for (ArtifactType artifactType : artifacts) {
283: if (!first) {
284: buf.append("\n");
285: }
286: first = false;
287: buf.append(PluginInstallerGBean.toArtifact(artifactType)
288: .toString());
289: }
290: return buf.toString();
291: }
292:
293: }
|