KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > console > repository > RepositoryViewPortlet


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.console.repository;
19
20 import org.apache.commons.fileupload.FileItem;
21 import org.apache.commons.fileupload.FileUploadException;
22 import org.apache.commons.fileupload.disk.DiskFileItemFactory;
23 import org.apache.commons.fileupload.portlet.PortletFileUpload;
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.geronimo.console.BasePortlet;
27 import org.apache.geronimo.console.util.PortletManager;
28 import org.apache.geronimo.kernel.Kernel;
29 import org.apache.geronimo.kernel.KernelRegistry;
30 import org.apache.geronimo.kernel.repository.Artifact;
31 import org.apache.geronimo.kernel.repository.FileWriteMonitor;
32 import org.apache.geronimo.kernel.repository.ListableRepository;
33 import org.apache.geronimo.kernel.repository.WriteableRepository;
34
35 import javax.portlet.ActionRequest;
36 import javax.portlet.ActionResponse;
37 import javax.portlet.PortletConfig;
38 import javax.portlet.PortletContext;
39 import javax.portlet.PortletException;
40 import javax.portlet.PortletRequestDispatcher;
41 import javax.portlet.RenderRequest;
42 import javax.portlet.RenderResponse;
43 import javax.portlet.WindowState;
44 import java.io.File JavaDoc;
45 import java.io.IOException JavaDoc;
46 import java.util.ArrayList JavaDoc;
47 import java.util.Arrays JavaDoc;
48 import java.util.Collections JavaDoc;
49 import java.util.Iterator JavaDoc;
50 import java.util.List JavaDoc;
51 import java.util.SortedSet JavaDoc;
52
53 /**
54  * @version $Rev: 480565 $ $Date: 2006-11-29 08:22:23 -0500 (Wed, 29 Nov 2006) $
55  */

56 public class RepositoryViewPortlet extends BasePortlet {
57
58     private final static Log log = LogFactory.getLog(RepositoryViewPortlet.class);
59
60     private Kernel kernel;
61
62     private PortletContext ctx;
63
64     private PortletRequestDispatcher normalView;
65
66     private PortletRequestDispatcher helpView;
67
68     private PortletRequestDispatcher usageView;
69
70     public void init(PortletConfig portletConfig) throws PortletException {
71         super.init(portletConfig);
72         kernel = KernelRegistry.getSingleKernel();
73         ctx = portletConfig.getPortletContext();
74         normalView = ctx
75                 .getRequestDispatcher("/WEB-INF/view/repository/normal.jsp");
76         helpView = ctx
77                 .getRequestDispatcher("/WEB-INF/view/repository/help.jsp");
78         usageView = ctx
79                 .getRequestDispatcher("/WEB-INF/view/repository/usage.jsp");
80     }
81
82     public void processAction(ActionRequest actionRequest,
83                               ActionResponse actionResponse) throws PortletException, IOException JavaDoc {
84         String JavaDoc action = actionRequest.getParameter("action");
85         if(action != null && action.equals("usage")) {
86             // User clicked on a repository entry to view usage
87
String JavaDoc res = actionRequest.getParameter("res");
88             actionResponse.setRenderParameter("mode", "usage");
89             actionResponse.setRenderParameter("res", res);
90             return;
91         }
92
93         try {
94
95
96             List JavaDoc list = new ArrayList JavaDoc();
97             WriteableRepository repo = PortletManager.getCurrentServer(actionRequest).getWritableRepositories()[0];
98
99             File JavaDoc uploadFile = null;
100             File JavaDoc file = null;
101             String JavaDoc name = null;
102             String JavaDoc basename = null;
103             String JavaDoc fileType = null;
104             String JavaDoc artifact = null;
105             String JavaDoc version = null;
106             String JavaDoc group = null;
107
108             PortletFileUpload uploader = new PortletFileUpload(new DiskFileItemFactory());
109             try {
110                 List JavaDoc items = uploader.parseRequest(actionRequest);
111                 for (Iterator JavaDoc i = items.iterator(); i.hasNext();) {
112                     FileItem item = (FileItem) i.next();
113                     if (!item.isFormField()) {
114                         String JavaDoc fieldName = item.getFieldName().trim();
115                         name = item.getName().trim();
116
117                         if (name.length() == 0) {
118                             file = null;
119                         } else {
120                             // IE sends full path while Firefox sends just basename
121
// in the case of "FullName" we may be able to infer the group
122
// Note, we can't use File.separatorChar because the file separator
123
// is dependent upon the client and not the server.
124
String JavaDoc fileChar = "\\";
125                             int fileNameIndex = name.lastIndexOf(fileChar);
126                             if (fileNameIndex == -1) {
127                                 fileChar = "/";
128                                 fileNameIndex = name.lastIndexOf(fileChar);
129                             }
130                             if (fileNameIndex != -1) {
131                                 basename = name.substring(fileNameIndex + 1);
132                             } else {
133                                 basename = name;
134                             }
135
136                             // Create the temporary file to be used for import to the server
137
file = File.createTempFile("geronimo-import", "");
138                             file.deleteOnExit();
139                             log.debug("Writing repository import file to " + file.getAbsolutePath());
140                         }
141
142                         if ("local".equals(fieldName)) {
143                             uploadFile = file;
144                         }
145
146                         if (file != null) {
147                             try {
148                                 item.write(file);
149                             } catch (Exception JavaDoc e) {
150                                 throw new PortletException(e);
151                             }
152                         }
153                         // This is not the file itself, but one of the form fields for the URI
154
} else {
155                         String JavaDoc fieldName = item.getFieldName().trim();
156                         if ("group".equals(fieldName)) {
157                             group = item.getString().trim();
158                         } else if ("artifact".equals(fieldName)) {
159                             artifact = item.getString().trim();
160                         } else if ("version".equals(fieldName)) {
161                             version = item.getString().trim();
162                         } else if ("fileType".equals(fieldName)) {
163                             fileType = item.getString().trim();
164                         }
165                     }
166                 }
167
168
169                 repo.copyToRepository(file, new Artifact(group, artifact, version, fileType), new FileWriteMonitor() {
170                     public void writeStarted(String JavaDoc fileDescription, int fileSize) {
171                         log.info("Copying into repository " + fileDescription + "...");
172                     }
173
174                     public void writeProgress(int bytes) {
175                     }
176
177                     public void writeComplete(int bytes) {
178                         log.info("Finished.");
179                     }
180                 });
181             } catch (FileUploadException e) {
182                 throw new PortletException(e);
183             }
184         } catch (PortletException e) {
185             throw e;
186         }
187     }
188
189     protected void doView(RenderRequest request, RenderResponse response)
190             throws PortletException, IOException JavaDoc {
191         // i think generic portlet already does this
192
if (WindowState.MINIMIZED.equals(request.getWindowState())) {
193             return;
194         }
195
196         String JavaDoc mode = request.getParameter("mode");
197         if(mode != null && mode.equals("usage")) {
198             String JavaDoc res = request.getParameter("res");
199             String JavaDoc[] parts = res.split("/");
200             request.setAttribute("res", res);
201             request.setAttribute("groupId", parts[0]);
202             request.setAttribute("artifactId", parts[1]);
203             request.setAttribute("version", parts[2]);
204             request.setAttribute("type", parts[3]);
205             usageView.include(request, response);
206             return;
207         }
208
209         try {
210             List JavaDoc list = new ArrayList JavaDoc();
211             ListableRepository[] repos = PortletManager.getCurrentServer(request).getRepositories();
212             for (int i = 0; i < repos.length; i++) {
213                 ListableRepository repo = repos[i];
214                 final SortedSet JavaDoc artifacts = repo.list();
215                 for (Iterator JavaDoc iterator = artifacts.iterator(); iterator.hasNext();) {
216                     String JavaDoc fileName = iterator.next().toString();
217                     list.add(fileName);
218                 }
219             }
220             Collections.sort(list);
221
222             request.setAttribute("org.apache.geronimo.console.repo.list", list);
223
224         } catch (Exception JavaDoc e) {
225             throw new PortletException(e);
226         }
227
228         normalView.include(request, response);
229     }
230
231     public void doHelp(RenderRequest request, RenderResponse response)
232             throws PortletException, IOException JavaDoc {
233         helpView.include(request, response);
234     }
235
236     public List JavaDoc listing(File JavaDoc dir, String JavaDoc basepath) throws java.io.IOException JavaDoc {
237         if (dir == null) {
238             throw new IllegalArgumentException JavaDoc("directory argument is null");
239         }
240
241         if (!dir.isDirectory()) {
242             throw new IllegalArgumentException JavaDoc("directory argument expected");
243         }
244
245         List JavaDoc listing = new ArrayList JavaDoc();
246
247         List JavaDoc ls = Arrays.asList(dir.listFiles());
248         Iterator JavaDoc iter = ls.iterator();
249
250         while (iter.hasNext()) {
251             File JavaDoc f = (File JavaDoc) iter.next();
252
253             if (f.isDirectory()) {
254                 List JavaDoc listing1 = listing(f, basepath);
255                 listing.addAll(listing1);
256             } else {
257                 listing.add(f.getCanonicalPath().substring(
258                         basepath.length() + 1));
259             }
260         }
261         return listing;
262     }
263
264 }
265
Popular Tags