001: package org.gridsphere.tools;
002:
003: import org.apache.tools.ant.BuildException;
004: import org.apache.tools.ant.Task;
005:
006: import java.io.*;
007: import java.util.Enumeration;
008: import java.util.List;
009: import java.util.Vector;
010: import java.util.jar.JarEntry;
011: import java.util.jar.JarFile;
012: import java.util.jar.JarOutputStream;
013:
014: public class DeployGridSphereTCK extends Task {
015:
016: private String warPath = null;
017: private String buildDir = null;
018: private String catalina = null;
019: private List portlets = new Vector();
020: private List portletapps = new Vector();
021:
022: private class WARFilenameFilter implements FilenameFilter {
023:
024: public boolean accept(File dir, String name) {
025: if (name.endsWith(".war"))
026: return true;
027: return false;
028: }
029: }
030:
031: public void setWarDir(String warDir) {
032: this .warPath = warDir;
033: //System.out.println("Setting configdir to: "+this.configDir);
034: }
035:
036: public void setBuildDir(String buildDir) {
037: this .buildDir = buildDir;
038: }
039:
040: public void setServer(String serverDir) {
041: this .catalina = serverDir;
042: }
043:
044: /**
045: * Tool to transform Sun TCK portlet WAR's to GridSphere JSR model
046: */
047: public void execute() throws BuildException {
048:
049: System.out
050: .println("GridSphere tool to deploy Sun TCK portlet WAR files as ");
051: System.out
052: .println(" GridSphere JSR portlet applications and create a test layout descriptor");
053:
054: try {
055: loadWars(warPath);
056: createLayout();
057: deployPortlets();
058: } catch (IOException e) {
059: System.err.println("Error converting WARS:");
060: e.printStackTrace();
061: }
062: }
063:
064: private void deployPortlets() throws IOException {
065: String portletsDir = catalina + File.separator + "webapps"
066: + File.separator + "gridsphere" + File.separator
067: + "WEB-INF" + File.separator + "CustomPortal"
068: + File.separator + "portlets" + File.separator;
069: File tmp = null;
070: for (int i = 0; i < portletapps.size(); i++) {
071: tmp = new File(portletsDir + portletapps.get(i));
072: tmp.createNewFile();
073: }
074: }
075:
076: private void createLayout() throws IOException {
077:
078: PrintWriter out = new PrintWriter(new BufferedWriter(
079: new FileWriter("guest-layout-tck.xml")));
080:
081: out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
082: out
083: .println("<page-layout theme=\"xp\" title=\"GridSphere Portal\">");
084: out.println("<portlet-header/>");
085: out
086: .print("<portlet-tabbed-pane selected=\"0\" style=\"menu\">\n"
087: + " <portlet-tab label=\"gridsphere\">\n"
088: + " <title lang=\"en\">GridSphere</title>");
089: System.err.println("Number of portlets: " + portlets.size());
090: for (int i = 0; i < portlets.size(); i++) {
091: System.err.println((String) portlets.get(i));
092: out.println("<portlet-frame>");
093: out.println("<portlet-class>" + (String) portlets.get(i)
094: + "</portlet-class>");
095: out.println("</portlet-frame>");
096: }
097: out
098: .println("</portlet-tab></portlet-tabbed-pane></page-layout");
099:
100: out.close();
101:
102: }
103:
104: private void loadWars(String warPath) throws IOException {
105:
106: File warDir = new File(warPath);
107:
108: if (!warDir.isDirectory()) {
109: throw new IOException("Specified TCK directory not valid: "
110: + warPath);
111: }
112:
113: String[] warFiles = warDir.list(new WARFilenameFilter());
114:
115: byte[] buffer = new byte[1024];
116: int bytesRead;
117:
118: // loop thru all WARs
119: for (int i = 0; i < warFiles.length; i++) {
120: //System.err.println(warPath + File.separator + warFiles[i].toString());
121:
122: String war = warFiles[i].substring(0, warFiles[i]
123: .indexOf(".war"));
124: portletapps.add(war);
125: JarFile jarFile = new JarFile(warPath + File.separator
126: + warFiles[i]);
127: JarOutputStream tempJar = new JarOutputStream(
128: new FileOutputStream(System
129: .getProperty("java.io.tmpdir")
130: + File.separator + warFiles[i].toString()));
131:
132: addGridSphereJSRDescriptor(tempJar);
133: addGridSphereTagLibs(tempJar);
134: addLogProps(tempJar);
135:
136: // loop thru all jars
137: Enumeration files = jarFile.entries();
138: while (files.hasMoreElements()) {
139: JarEntry entry = (JarEntry) files.nextElement();
140:
141: //System.err.println("\t\t" + entry.getName());
142: if (entry.getName().equals("WEB-INF/web.xml")) {
143: InputStream entryStream = jarFile
144: .getInputStream(entry);
145: modifyWebXML(entryStream, tempJar, warFiles[i]);
146: break;
147: }
148: if (entry.getName().equals("WEB-INF/portlet.xml")) {
149: InputStream entryStream = jarFile
150: .getInputStream(entry);
151: collectPortletNames(war, entryStream);
152: }
153:
154: tempJar.putNextEntry(entry);
155:
156: InputStream entryStream = jarFile.getInputStream(entry);
157:
158: // Read the entry and write it to the temp jar.
159: while ((bytesRead = entryStream.read(buffer)) != -1) {
160: tempJar.write(buffer, 0, bytesRead);
161: }
162:
163: }
164:
165: tempJar.close();
166: jarFile.close();
167: }
168:
169: }
170:
171: public synchronized void modifyWebXML(InputStream webxmlStream,
172: JarOutputStream tempJar, String warname) throws IOException {
173: BufferedReader bis = new BufferedReader(new InputStreamReader(
174: webxmlStream));
175: String line = null;
176: String war = warname.substring(0, warname.indexOf(".war"));
177: JarEntry entry = new JarEntry("WEB-INF" + File.separator
178: + "web.xml");
179: tempJar.putNextEntry(entry);
180:
181: PrintWriter out = new PrintWriter(new BufferedWriter(
182: new FileWriter("tmp.xml")));
183: boolean hasServletEntry = false;
184: boolean domapping = false;
185: while ((line = bis.readLine()) != null) {
186: if (line.endsWith("<servlet-mapping>")
187: && (!hasServletEntry)) {
188: out.println(" <servlet>");
189: out
190: .println(" <servlet-name>PortletServlet</servlet-name>");
191: out
192: .println(" <servlet-class>org.gridsphere.provider.portlet.jsr.PortletServlet</servlet-class>");
193: out.println(" </servlet>");
194: out.println(line);
195: domapping = true;
196: hasServletEntry = true;
197: } else if (line.endsWith("<session-config>")
198: && (!hasServletEntry)) {
199: out.println(" <servlet>");
200: out
201: .println(" <servlet-name>PortletServlet</servlet-name>");
202: out
203: .println(" <servlet-class>org.gridsphere.provider.portlet.jsr.PortletServlet</servlet-class>");
204: out.println(" </servlet>");
205: out.println(" <servlet-mapping>");
206: out
207: .println(" <servlet-name>PortletServlet</servlet-name>");
208: out.println(" <url-pattern>/jsr/" + war
209: + "</url-pattern>");
210: out.println(" </servlet-mapping>");
211: out.println(line);
212: hasServletEntry = true;
213: } else if ((line.endsWith("</web-app>"))
214: && (!hasServletEntry)) {
215: out.println(" <servlet>");
216: out
217: .println(" <servlet-name>PortletServlet</servlet-name>");
218: out
219: .println(" <servlet-class>org.gridsphere.provider.portlet.jsr.PortletServlet</servlet-class>");
220: out.println(" </servlet>");
221: out.println(" <servlet-mapping>");
222: out
223: .println(" <servlet-name>PortletServlet</servlet-name>");
224: out.println(" <url-pattern>/jsr/" + war
225: + "</url-pattern>");
226: out.println(" </servlet-mapping>");
227: out.println("</web-app>");
228: } else if ((domapping)
229: && (line.endsWith("<session-config>") || (line
230: .endsWith("<web-app>")))) {
231: out.println(" <servlet-mapping>");
232: out
233: .println(" <servlet-name>PortletServlet</servlet-name>");
234: out.println(" <url-pattern>/jsr/" + war
235: + "</url-pattern>");
236: out.println(" </servlet-mapping>");
237: out.println(line);
238: hasServletEntry = true;
239: } else {
240: out.println(line);
241: }
242: }
243: out.close();
244: bis.close();
245:
246: // Open the given file.
247:
248: FileInputStream file = new FileInputStream("tmp.xml");
249: byte[] buffer = new byte[1024];
250: int bytesRead;
251:
252: try {
253:
254: // Read the file and write it to the jar.
255:
256: while ((bytesRead = file.read(buffer)) != -1) {
257: tempJar.write(buffer, 0, bytesRead);
258: }
259:
260: System.out.println(entry.getName() + " added.");
261: } finally {
262: file.close();
263: }
264:
265: }
266:
267: public void addGridSphereJSRDescriptor(JarOutputStream tempJar)
268: throws IOException {
269:
270: String fileName = "config" + File.separator + "template"
271: + File.separator + "gridsphere-portlet-jsr.xml";
272: byte[] buffer = new byte[1024];
273: int bytesRead;
274:
275: // Open the given file.
276:
277: FileInputStream file = new FileInputStream(fileName);
278:
279: try {
280: // Create a jar entry and add it to the temp jar.
281:
282: JarEntry entry = new JarEntry("WEB-INF" + File.separator
283: + "gridsphere-portlet.xml");
284: tempJar.putNextEntry(entry);
285:
286: // Read the file and write it to the jar.
287:
288: while ((bytesRead = file.read(buffer)) != -1) {
289: tempJar.write(buffer, 0, bytesRead);
290: }
291:
292: System.out.println(entry.getName() + " added.");
293:
294: } finally {
295: file.close();
296: }
297: }
298:
299: public void addLogProps(JarOutputStream tempJar) throws IOException {
300:
301: String fileName = "config" + File.separator
302: + "log4j.properties";
303: byte[] buffer = new byte[1024];
304: int bytesRead;
305:
306: // Open the given file.
307:
308: FileInputStream file = new FileInputStream(fileName);
309:
310: try {
311: // Create a jar entry and add it to the temp jar.
312:
313: JarEntry entry = new JarEntry("WEB-INF" + File.separator
314: + "classes" + File.separator + "log4j.properties");
315: tempJar.putNextEntry(entry);
316:
317: // Read the file and write it to the jar.
318:
319: while ((bytesRead = file.read(buffer)) != -1) {
320: tempJar.write(buffer, 0, bytesRead);
321: }
322:
323: System.out.println(entry.getName() + " added.");
324:
325: } finally {
326: file.close();
327: }
328: }
329:
330: public void addGridSphereTagLibs(JarOutputStream tempJar)
331: throws IOException {
332:
333: String fileName = buildDir + File.separator + "lib"
334: + File.separator + "gridsphere-ui-tags-2.1.jar";
335: byte[] buffer = new byte[1024];
336: int bytesRead;
337:
338: // Open the given file.
339:
340: FileInputStream file = new FileInputStream(fileName);
341:
342: try {
343: // Create a jar entry and add it to the temp jar.
344:
345: JarEntry entry = new JarEntry("WEB-INF" + File.separator
346: + "lib" + File.separator
347: + "gridsphere-ui-tags-2.1.jar");
348: tempJar.putNextEntry(entry);
349:
350: // Read the file and write it to the jar.
351:
352: while ((bytesRead = file.read(buffer)) != -1) {
353: tempJar.write(buffer, 0, bytesRead);
354: }
355:
356: System.out.println(entry.getName() + " added.");
357:
358: } finally {
359: file.close();
360: }
361: }
362:
363: public void collectPortletNames(String war,
364: InputStream portletxmlStream) throws IOException {
365: BufferedReader bis = new BufferedReader(new InputStreamReader(
366: portletxmlStream));
367: String line = null;
368: String portlet = "";
369: while ((line = bis.readLine()) != null) {
370: //System.err.println("portlet= " + line);
371: if (line.indexOf("<portlet-name>") > 0) {
372: int d = line.indexOf("<portlet-name>");
373: String p = line
374: .substring(d + "<portlet-name>".length());
375: int e = p.indexOf("</portlet-name>");
376: portlet = p.substring(0, e);
377: portlets.add(war + "#" + portlet);
378: }
379: }
380: bis.close();
381: }
382: }
|