From: Marc Englund Date: Thu, 17 Apr 2008 07:38:21 +0000 (+0000) Subject: Refactored. X-Git-Tag: 6.7.0.beta1~4884 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=a4b3618efe4616359cab037992f664634eb0019f;p=vaadin-framework.git Refactored. Created PortletConfigurationGenerator that generates portlet.xml and liferay xml:s from web.xml. svn changeset:4189/svn branch:trunk --- diff --git a/portlet-src/com/itmill/toolkit/demo/PortletDemo.java b/portlet-src/com/itmill/toolkit/demo/PortletDemo.java deleted file mode 100644 index e4089612f3..0000000000 --- a/portlet-src/com/itmill/toolkit/demo/PortletDemo.java +++ /dev/null @@ -1,152 +0,0 @@ -/** - * - */ -package com.itmill.toolkit.demo; - -import java.util.Iterator; -import java.util.Map; - -import javax.portlet.ActionRequest; -import javax.portlet.ActionResponse; -import javax.portlet.PortletMode; -import javax.portlet.PortletRequest; -import javax.portlet.PortletURL; -import javax.portlet.RenderRequest; -import javax.portlet.RenderResponse; -import javax.portlet.WindowState; - -import com.itmill.toolkit.Application; -import com.itmill.toolkit.terminal.ExternalResource; -import com.itmill.toolkit.terminal.gwt.server.PortletApplicationContext; -import com.itmill.toolkit.terminal.gwt.server.PortletApplicationContext.PortletListener; -import com.itmill.toolkit.ui.Label; -import com.itmill.toolkit.ui.Link; -import com.itmill.toolkit.ui.TextField; -import com.itmill.toolkit.ui.Window; -import com.itmill.toolkit.ui.Window.Notification; - -/** - * @author marc - * - */ -public class PortletDemo extends Application { - - Window main = new Window(); - TextField tf = new TextField("Some value"); - Label userInfo = new Label(); - Link portletEdit = new Link(); - Link portletMax = new Link(); - Link someAction = null; - - public void init() { - main = new Window(); - setMainWindow(main); - - userInfo.setCaption("User info"); - userInfo.setContentMode(Label.CONTENT_PREFORMATTED); - main.addComponent(userInfo); - - tf.setEnabled(false); - tf.setImmediate(true); - main.addComponent(tf); - - portletEdit.setEnabled(false); - main.addComponent(portletEdit); - portletMax.setEnabled(false); - main.addComponent(portletMax); - - if (getContext() instanceof PortletApplicationContext) { - PortletApplicationContext ctx = (PortletApplicationContext) getContext(); - ctx.addPortletListener(this, new DemoPortletListener()); - } else { - getMainWindow().showNotification("Not inited via Portal!", - Notification.TYPE_ERROR_MESSAGE); - } - - } - - private class DemoPortletListener implements PortletListener { - - public void handleActionRequest(ActionRequest request, - ActionResponse response) { - - main.addComponent(new Label("Action received")); - - } - - public void handleRenderRequest(RenderRequest request, - RenderResponse response) { - // Portlet up-and-running, enable stuff - portletEdit.setEnabled(true); - portletMax.setEnabled(true); - - // Editable if we're in editmode - tf.setEnabled((request.getPortletMode() == PortletMode.EDIT)); - - // Show notification about current mode and state - getMainWindow().showNotification( - "Portlet status", - "Mode: " + request.getPortletMode() + " State: " - + request.getWindowState(), - Notification.TYPE_WARNING_MESSAGE); - - // Display current user info - Map uinfo = (Map) request.getAttribute(PortletRequest.USER_INFO); - if (uinfo != null) { - String s = ""; - for (Iterator it = uinfo.keySet().iterator(); it.hasNext();) { - Object key = it.next(); - Object val = uinfo.get(key); - s += key + ": " + val + "\n"; - } - if (request.isUserInRole("administrator")) { - s += "(administrator)"; - } - userInfo.setValue(s); - } else { - userInfo.setValue("-"); - } - - // Create Edit/Done link (actionUrl) - PortletURL url = response.createActionURL(); - try { - url - .setPortletMode((request.getPortletMode() == PortletMode.VIEW ? PortletMode.EDIT - : PortletMode.VIEW)); - portletEdit.setResource(new ExternalResource(url.toString())); - portletEdit - .setCaption((request.getPortletMode() == PortletMode.VIEW ? "Edit" - : "Done")); - } catch (Exception e) { - portletEdit.setEnabled(false); - } - // Create Maximize/Normal link (actionUrl) - url = response.createActionURL(); - try { - url - .setWindowState((request.getWindowState() == WindowState.NORMAL ? WindowState.MAXIMIZED - : WindowState.NORMAL)); - portletMax.setResource(new ExternalResource(url.toString())); - portletMax - .setCaption((request.getWindowState() == WindowState.NORMAL ? "Maximize" - : "Back to normal")); - } catch (Exception e) { - portletMax.setEnabled(false); - } - - if (someAction == null) { - url = response.createActionURL(); - try { - someAction = new Link("An action", new ExternalResource(url - .toString())); - main.addComponent(someAction); - } catch (Exception e) { - // Oops - System.err.println("Could not create someAction: " + e); - } - - } - - } - } -} diff --git a/portlet-src/com/itmill/toolkit/demo/portlet/PortletDemo.java b/portlet-src/com/itmill/toolkit/demo/portlet/PortletDemo.java new file mode 100644 index 0000000000..8645be21b7 --- /dev/null +++ b/portlet-src/com/itmill/toolkit/demo/portlet/PortletDemo.java @@ -0,0 +1,152 @@ +/** + * + */ +package com.itmill.toolkit.demo.portlet; + +import java.util.Iterator; +import java.util.Map; + +import javax.portlet.ActionRequest; +import javax.portlet.ActionResponse; +import javax.portlet.PortletMode; +import javax.portlet.PortletRequest; +import javax.portlet.PortletURL; +import javax.portlet.RenderRequest; +import javax.portlet.RenderResponse; +import javax.portlet.WindowState; + +import com.itmill.toolkit.Application; +import com.itmill.toolkit.terminal.ExternalResource; +import com.itmill.toolkit.terminal.gwt.server.PortletApplicationContext; +import com.itmill.toolkit.terminal.gwt.server.PortletApplicationContext.PortletListener; +import com.itmill.toolkit.ui.Label; +import com.itmill.toolkit.ui.Link; +import com.itmill.toolkit.ui.TextField; +import com.itmill.toolkit.ui.Window; +import com.itmill.toolkit.ui.Window.Notification; + +/** + * @author marc + * + */ +public class PortletDemo extends Application { + + Window main = new Window(); + TextField tf = new TextField("Some value"); + Label userInfo = new Label(); + Link portletEdit = new Link(); + Link portletMax = new Link(); + Link someAction = null; + + public void init() { + main = new Window(); + setMainWindow(main); + + userInfo.setCaption("User info"); + userInfo.setContentMode(Label.CONTENT_PREFORMATTED); + main.addComponent(userInfo); + + tf.setEnabled(false); + tf.setImmediate(true); + main.addComponent(tf); + + portletEdit.setEnabled(false); + main.addComponent(portletEdit); + portletMax.setEnabled(false); + main.addComponent(portletMax); + + if (getContext() instanceof PortletApplicationContext) { + PortletApplicationContext ctx = (PortletApplicationContext) getContext(); + ctx.addPortletListener(this, new DemoPortletListener()); + } else { + getMainWindow().showNotification("Not inited via Portal!", + Notification.TYPE_ERROR_MESSAGE); + } + + } + + private class DemoPortletListener implements PortletListener { + + public void handleActionRequest(ActionRequest request, + ActionResponse response) { + + main.addComponent(new Label("Action received")); + + } + + public void handleRenderRequest(RenderRequest request, + RenderResponse response) { + // Portlet up-and-running, enable stuff + portletEdit.setEnabled(true); + portletMax.setEnabled(true); + + // Editable if we're in editmode + tf.setEnabled((request.getPortletMode() == PortletMode.EDIT)); + + // Show notification about current mode and state + getMainWindow().showNotification( + "Portlet status", + "Mode: " + request.getPortletMode() + " State: " + + request.getWindowState(), + Notification.TYPE_WARNING_MESSAGE); + + // Display current user info + Map uinfo = (Map) request.getAttribute(PortletRequest.USER_INFO); + if (uinfo != null) { + String s = ""; + for (Iterator it = uinfo.keySet().iterator(); it.hasNext();) { + Object key = it.next(); + Object val = uinfo.get(key); + s += key + ": " + val + "\n"; + } + if (request.isUserInRole("administrator")) { + s += "(administrator)"; + } + userInfo.setValue(s); + } else { + userInfo.setValue("-"); + } + + // Create Edit/Done link (actionUrl) + PortletURL url = response.createActionURL(); + try { + url + .setPortletMode((request.getPortletMode() == PortletMode.VIEW ? PortletMode.EDIT + : PortletMode.VIEW)); + portletEdit.setResource(new ExternalResource(url.toString())); + portletEdit + .setCaption((request.getPortletMode() == PortletMode.VIEW ? "Edit" + : "Done")); + } catch (Exception e) { + portletEdit.setEnabled(false); + } + // Create Maximize/Normal link (actionUrl) + url = response.createActionURL(); + try { + url + .setWindowState((request.getWindowState() == WindowState.NORMAL ? WindowState.MAXIMIZED + : WindowState.NORMAL)); + portletMax.setResource(new ExternalResource(url.toString())); + portletMax + .setCaption((request.getWindowState() == WindowState.NORMAL ? "Maximize" + : "Back to normal")); + } catch (Exception e) { + portletMax.setEnabled(false); + } + + if (someAction == null) { + url = response.createActionURL(); + try { + someAction = new Link("An action", new ExternalResource(url + .toString())); + main.addComponent(someAction); + } catch (Exception e) { + // Oops + System.err.println("Could not create someAction: " + e); + } + + } + + } + } +} diff --git a/portlet-src/com/itmill/toolkit/portlet/util/PortletConfigurationGenerator.java b/portlet-src/com/itmill/toolkit/portlet/util/PortletConfigurationGenerator.java new file mode 100644 index 0000000000..175e8bc592 --- /dev/null +++ b/portlet-src/com/itmill/toolkit/portlet/util/PortletConfigurationGenerator.java @@ -0,0 +1,269 @@ +/** + * + */ +package com.itmill.toolkit.portlet.util; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.FileReader; +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.nio.charset.Charset; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * Generates portlet.xml, liferay-portlet.xml, liferay-display.xml from web.xml. + * Currently uses regular expressions to avoid dependencies; does not strictly + * adhere to xml rules, but should work with a 'normal' web.xml. + * + * @author marc + */ +public class PortletConfigurationGenerator { + // can be changed for debugging: + private static final String WEB_XML_FILE = "web.xml"; + private static final String PORTLET_XML_FILE = "portlet.xml"; + private static final String LIFERAY_PORTLET_XML_FILE = "liferay-portlet.xml"; + private static final String LIFERAY_DISPLAY_XML_FILE = "liferay-display.xml"; + + // "templates" follow; + private static final String PORTLET_XML_HEAD = "\r\n" + + "\r\n"; + private static final String PORTLET_XML_SECTION = " \r\n" + + " %PORTLETNAME%\r\n" + + " IT Mill Toolkit %NAME%\r\n" + + " com.itmill.toolkit.terminal.gwt.server.ApplicationPortlet\r\n" + + " \r\n" + + " application\r\n" + + " %URL%\r\n" + + " \r\n" + + " \r\n" + + " text/html\r\n" + + " view\r\n" + + " edit\r\n" + + " help\r\n" + + " \r\n" + + " \r\n" + + " IT Mill Toolkit %NAME%\r\n" + + " %NAME%\r\n" + + " \r\n" + + " \r\n" + + " \r\n" + + " administrator\r\n" + + " \r\n" + + " \r\n" + + " guest\r\n" + + " \r\n" + + " \r\n" + + " power-user\r\n" + + " \r\n" + + " \r\n" + + " user\r\n" + + " \r\n" + + " \r\n"; + private static final String PORTLET_XML_FOOT = ""; + + private static final String LIFERAY_PORTLET_XML_HEAD = "\r\n" + + "\r\n" + + "\r\n" + "\r\n" + ""; + private static final String LIFERAY_PORTLET_XML_SECTION = " \r\n" + + " %PORTLETNAME%\r\n" + + " true \r\n" + + " false\r\n" + + " \r\n" + ""; + private static final String LIFERAY_PORTLET_XML_FOOT = " \r\n" + + " \r\n" + + " administrator\r\n" + + " Administrator\r\n" + + " \r\n" + " \r\n" + + " guest\r\n" + + " Guest\r\n" + + " \r\n" + " \r\n" + + " power-user\r\n" + + " Power User\r\n" + + " \r\n" + " \r\n" + + " user\r\n" + + " User\r\n" + + " \r\n" + " \r\n" + + ""; + private static final String LIFERAY_DISPLAY_XML_HEAD = "\r\n" + + "\r\n" + + "\r\n" + + "\r\n" + + " \r\n" + ""; + private static final String LIFERAY_DISPLAY_XML_SECTION = " \r\n"; + private static final String LIFERAY_DISPLAY_XML_FOOT = "\r\n" + + " \r\n" + ""; + + /** + * @param args + * + */ + public static void main(String[] args) { + if (args.length < 1 || !new File(args[0]).isDirectory()) { + System.err + .println("Usage: PortletConfigurationGenerator "); + return; + } + + /* + * Read web.xml + */ + File dir = new File(args[0]); + File webxmlFile = new File(dir.getAbsolutePath() + File.separatorChar + + WEB_XML_FILE); + String webXml = ""; + BufferedReader in = null; + try { + in = new BufferedReader(new FileReader(webxmlFile)); + String line = in.readLine(); + while (line != null) { + webXml += line; + line = in.readLine(); + } + } catch (FileNotFoundException e1) { + System.out.println(webxmlFile + " not found!"); + return; + } catch (IOException e2) { + System.out.println("IOException while reading " + webxmlFile); + webXml = null; + } + try { + if (in != null) { + in.close(); + } + } catch (IOException e1) { + System.out.println("IOException while closing " + webxmlFile); + } + if (webXml == null) { + System.out.println("Could not read web.xml!"); + return; + } + + /* + * Open outputs + */ + + // Open portlet.xml + File portletXmlFile = new File(args[0] + File.separatorChar + + PORTLET_XML_FILE); + OutputStreamWriter pout = null; + try { + pout = new OutputStreamWriter(new FileOutputStream(portletXmlFile), + Charset.forName("UTF-8")); + } catch (FileNotFoundException e) { + System.out.println(portletXmlFile + " not found!"); + } + // open liferay-portlet.xml + File liferayPortletXmlFile = new File(args[0] + File.separatorChar + + LIFERAY_PORTLET_XML_FILE); + OutputStreamWriter lpout = null; + try { + lpout = new OutputStreamWriter(new FileOutputStream( + liferayPortletXmlFile), Charset.forName("UTF-8")); + } catch (FileNotFoundException e) { + System.out.println(liferayPortletXmlFile + " not found!"); + } + // open liferay-display.xml + File liferayDisplayXmlFile = new File(args[0] + File.separatorChar + + LIFERAY_DISPLAY_XML_FILE); + OutputStreamWriter ldout = null; + try { + ldout = new OutputStreamWriter(new FileOutputStream( + liferayDisplayXmlFile), Charset.forName("UTF-8")); + } catch (FileNotFoundException e) { + System.out.println(liferayDisplayXmlFile + " not found!"); + } + + if (pout != null && lpout != null && ldout != null) { + + String pstring = PORTLET_XML_HEAD; + String lpstring = LIFERAY_PORTLET_XML_HEAD; + String ldstring = LIFERAY_DISPLAY_XML_HEAD; + + Pattern p = Pattern + .compile( + ".*?(.*?)<\\/servlet-name>.*?(.*?)<\\/url-pattern>.*?<\\/servlet-mapping>", + Pattern.MULTILINE); + Matcher m = p.matcher(webXml); + while (m.find()) { + if (m.groupCount() != 2) { + System.out + .println("Could not find servlet-name and url-pattern for: " + + m.group()); + continue; + } + String name = m.group(1); + // remove leading- and trailing whitespace + name = name.replaceAll("^\\s*", ""); + name = name.replaceAll("\\s*$", ""); + String pname = name + "Portlet"; + String url = m.group(2); + // remove leading- and trailing whitespace + url = url.replaceAll("^\\s*", ""); + url = url.replaceAll("\\s*$", ""); + if (url.startsWith("/")) { + url = url.substring(1); + } + if (url.endsWith("*")) { + url = url.substring(0, url.length() - 1); + } + if (url.endsWith("/")) { + url = url.substring(0, url.length() - 1); + } + System.out.println("Mapping " + pname + " to " + url); + String s = PORTLET_XML_SECTION; + s = s.replaceAll("%NAME%", name); + s = s.replaceAll("%PORTLETNAME%", pname); + s = s.replaceAll("%URL%", url); + pstring += s; + + s = LIFERAY_PORTLET_XML_SECTION; + s = s.replaceAll("%NAME%", name); + s = s.replaceAll("%PORTLETNAME%", pname); + s = s.replaceAll("%URL%", url); + lpstring += s; + + s = LIFERAY_DISPLAY_XML_SECTION; + s = s.replaceAll("%NAME%", name); + s = s.replaceAll("%PORTLETNAME%", pname); + s = s.replaceAll("%URL%", url); + ldstring += s; + + } + + pstring += PORTLET_XML_FOOT; + lpstring += LIFERAY_PORTLET_XML_FOOT; + ldstring += LIFERAY_DISPLAY_XML_FOOT; + + try { + pout.write(pstring); + lpout.write(lpstring); + ldout.write(ldstring); + } catch (IOException e) { + System.out.println("Write FAILED:" + e); + } + + } + + try { + if (pout != null) { + pout.close(); + } + if (lpout != null) { + lpout.close(); + } + if (ldout != null) { + ldout.close(); + } + } catch (IOException e) { + System.out.println("Close FAILED: " + e); + } + System.out.println("Done."); + } +} diff --git a/portlet-src/com/itmill/toolkit/terminal/gwt/server/ApplicationPortlet.java b/portlet-src/com/itmill/toolkit/terminal/gwt/server/ApplicationPortlet.java index e21a1b01f1..eebc38330b 100644 --- a/portlet-src/com/itmill/toolkit/terminal/gwt/server/ApplicationPortlet.java +++ b/portlet-src/com/itmill/toolkit/terminal/gwt/server/ApplicationPortlet.java @@ -17,7 +17,7 @@ import com.itmill.toolkit.Application; public class ApplicationPortlet implements Portlet { // The application to show - protected String app = "Calc"; + protected String app = null; // some applications might require that the height is specified protected String height = null; // e.g "200px"