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