You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PortletConfigurationGenerator.java 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /**
  2. *
  3. */
  4. package com.vaadin.buildhelpers;
  5. import java.io.BufferedReader;
  6. import java.io.File;
  7. import java.io.FileNotFoundException;
  8. import java.io.FileOutputStream;
  9. import java.io.FileReader;
  10. import java.io.IOException;
  11. import java.io.OutputStreamWriter;
  12. import java.nio.charset.Charset;
  13. import java.util.regex.Matcher;
  14. import java.util.regex.Pattern;
  15. /**
  16. * Generates portlet.xml, liferay-portlet.xml, liferay-display.xml from web.xml.
  17. * Currently uses regular expressions to avoid dependencies; does not strictly
  18. * adhere to xml rules, but should work with a 'normal' web.xml.
  19. *
  20. * To be included, the servlet-mapping must include a special comment: <!--
  21. * portlet --> If the portlet requires some special styles (i.e height): <!--
  22. * portlet style=height:400px -->
  23. *
  24. * @author marc
  25. */
  26. public class PortletConfigurationGenerator {
  27. // can be changed for debugging:
  28. private static final String WEB_XML_FILE = "web.xml";
  29. private static final String PORTLET_XML_FILE = "portlet.xml";
  30. private static final String LIFERAY_PORTLET_XML_FILE = "liferay-portlet.xml";
  31. private static final String LIFERAY_DISPLAY_XML_FILE = "liferay-display.xml";
  32. private static final String JBOSS_OBJECT_FILE = "itmill-object.xml";
  33. private static final String JBOSS_INSTANCE_FILE = "portlet-instances.xml";
  34. // "templates" follow;
  35. private static final String PORTLET_XML_HEAD = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
  36. + "<portlet-app\n"
  37. + " xmlns=\"http://java.sun.com/xml/ns/portlet/portlet-app_1_0.xsd\"\n"
  38. + " version=\"1.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"
  39. + " xsi:schemaLocation=\"http://java.sun.com/xml/ns/portlet/portlet-app_1_0.xsd http://java.sun.com/xml/ns/portlet/portlet-app_1_0.xsd\">\n";
  40. private static final String PORTLET_XML_SECTION = " <portlet>\n"
  41. + " <portlet-name>%PORTLETNAME%</portlet-name>\n"
  42. + " <display-name>IT Mill Toolkit %NAME%</display-name>\n"
  43. + " <portlet-class>com.vaadin.terminal.gwt.server.ApplicationPortlet</portlet-class>\n"
  44. + " <init-param>\n"
  45. + " <name>application</name>\n"
  46. + " <value>%URL%</value>\n"
  47. + " </init-param>\n"
  48. + " %EXTRAPARAMS%\n"
  49. + " <supports>\n"
  50. + " <mime-type>text/html</mime-type>\n"
  51. + " <portlet-mode>view</portlet-mode>\n"
  52. + " <portlet-mode>edit</portlet-mode>\n"
  53. + " <portlet-mode>help</portlet-mode>\n"
  54. + " </supports>\n"
  55. + " <portlet-info>\n"
  56. + " <title>%NAME%</title>\n"
  57. + " <short-title>%NAME%</short-title>\n"
  58. + " </portlet-info>\n" + " \n"
  59. + " <security-role-ref>\n"
  60. + " <role-name>administrator</role-name>\n"
  61. + " </security-role-ref>\n"
  62. + " <security-role-ref>\n"
  63. + " <role-name>guest</role-name>\n"
  64. + " </security-role-ref>\n"
  65. + " <security-role-ref>\n"
  66. + " <role-name>power-user</role-name>\n"
  67. + " </security-role-ref>\n"
  68. + " <security-role-ref>\n"
  69. + " <role-name>user</role-name>\n"
  70. + " </security-role-ref>\n" + " </portlet>\n";
  71. private static final String PORTLET_XML_FOOT = "\n" + "</portlet-app>";
  72. private static final String LIFERAY_PORTLET_XML_HEAD = "<?xml version=\"1.0\"?>\n"
  73. + "<!DOCTYPE liferay-portlet-app PUBLIC \"-//Liferay//DTD Portlet Application 4.3.0//EN\" \"http://www.liferay.com/dtd/liferay-portlet-app_4_3_0.dtd\">\n"
  74. + "\n" + "<liferay-portlet-app>\n" + "";
  75. private static final String LIFERAY_PORTLET_XML_SECTION = " <portlet>\n"
  76. + " <portlet-name>%PORTLETNAME%</portlet-name>\n"
  77. + " <instanceable>true</instanceable> \n"
  78. + " <ajaxable>false</ajaxable>\n"
  79. + " </portlet>\n" + "";
  80. private static final String LIFERAY_PORTLET_XML_FOOT = " \n"
  81. + " <role-mapper>\n"
  82. + " <role-name>administrator</role-name>\n"
  83. + " <role-link>Administrator</role-link>\n"
  84. + " </role-mapper>\n" + " <role-mapper>\n"
  85. + " <role-name>guest</role-name>\n"
  86. + " <role-link>Guest</role-link>\n"
  87. + " </role-mapper>\n" + " <role-mapper>\n"
  88. + " <role-name>power-user</role-name>\n"
  89. + " <role-link>Power User</role-link>\n"
  90. + " </role-mapper>\n" + " <role-mapper>\n"
  91. + " <role-name>user</role-name>\n"
  92. + " <role-link>User</role-link>\n"
  93. + " </role-mapper>\n" + " \n"
  94. + "</liferay-portlet-app>";
  95. private static final String LIFERAY_DISPLAY_XML_HEAD = "<?xml version=\"1.0\"?>\n"
  96. + "<!DOCTYPE display PUBLIC \"-//Liferay//DTD Display 4.0.0//EN\" \"http://www.liferay.com/dtd/liferay-display_4_0_0.dtd\">\n"
  97. + "\n"
  98. + "<display>\n"
  99. + " <category name=\"IT Mill Toolkit\">\n" + "";
  100. private static final String LIFERAY_DISPLAY_XML_SECTION = " <portlet id=\"%PORTLETNAME%\" />\n";
  101. private static final String LIFERAY_DISPLAY_XML_FOOT = "\n"
  102. + " </category>\n" + "</display>";
  103. private static final String JBOSS_INSTANCE_HEAD = "<?xml version=\"1.0\" standalone=\"yes\"?>\r\n"
  104. + "<!DOCTYPE deployments PUBLIC\r\n"
  105. + " \"-//JBoss Portal//DTD Portlet Instances 2.6//EN\"\r\n"
  106. + " \"http://www.jboss.org/portal/dtd/portlet-instances_2_6.dtd\">\r\n"
  107. + "<deployments>\r\n";
  108. private static final String JBOSS_INSTANCE_SECTION = " <deployment>\r\n <instance>\r\n"
  109. + " <instance-id>%PORTLETNAME%Instance</instance-id>\r\n"
  110. + " <portlet-ref>%PORTLETNAME%</portlet-ref>\r\n"
  111. + " </instance>\r\n </deployment>\r\n";
  112. private static final String JBOSS_INSTANCE_FOOT = "</deployments>";
  113. private static final String JBOSS_OBJECT_HEAD = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
  114. + "<!DOCTYPE deployments PUBLIC\r\n"
  115. + " \"-//JBoss Portal//DTD Portal Object 2.6//EN\"\r\n"
  116. + " \"http://www.jboss.org/portal/dtd/portal-object_2_6.dtd\">\r\n"
  117. + "<deployments>\r\n";
  118. private static final String JBOSS_OBJECT_SECTION = " <deployment>\r\n"
  119. + " <parent-ref>default.default</parent-ref>\r\n"
  120. + " <if-exists>overwrite</if-exists>"
  121. + " <window>\r\n"
  122. + " <window-name>%PORTLETNAME%Window</window-name>\r\n"
  123. + " <content>\r\n"
  124. + " <content-type>portlet</content-type>\r\n"
  125. + " <content-uri>%PORTLETNAME%Instance</content-uri>\r\n"
  126. + " </content>\r\n"
  127. + " <region>center</region>\r\n"
  128. + " <height>1</height>\r\n"
  129. + " </window>\r\n </deployment>\r\n";
  130. private static final String JBOSS_OBJECT_FOOT = "</deployments>";
  131. /**
  132. * @param args
  133. * <path to directory with web.xml> [default widgetset to use]
  134. */
  135. public static void main(String[] args) {
  136. if (args.length < 1 || !new File(args[0]).isDirectory()) {
  137. System.err
  138. .println("Usage: PortletConfigurationGenerator <directory> [widgetset]");
  139. return;
  140. }
  141. String widgetset = "";
  142. if (args.length > 1) {
  143. widgetset = args[1];
  144. }
  145. /*
  146. * Read web.xml
  147. */
  148. File dir = new File(args[0]);
  149. File webxmlFile = new File(dir.getAbsolutePath() + File.separatorChar
  150. + WEB_XML_FILE);
  151. String webXml = "";
  152. BufferedReader in = null;
  153. try {
  154. in = new BufferedReader(new FileReader(webxmlFile));
  155. String line = in.readLine();
  156. while (line != null) {
  157. webXml += line;
  158. line = in.readLine();
  159. }
  160. } catch (FileNotFoundException e1) {
  161. System.out.println(webxmlFile + " not found!");
  162. return;
  163. } catch (IOException e2) {
  164. System.out.println("IOException while reading " + webxmlFile);
  165. webXml = null;
  166. }
  167. try {
  168. if (in != null) {
  169. in.close();
  170. }
  171. } catch (IOException e1) {
  172. System.out.println("IOException while closing " + webxmlFile);
  173. }
  174. if (webXml == null) {
  175. System.out.println("Could not read web.xml!");
  176. return;
  177. }
  178. /*
  179. * Open outputs
  180. */
  181. // Open portlet.xml
  182. File portletXmlFile = new File(args[0] + File.separatorChar
  183. + PORTLET_XML_FILE);
  184. OutputStreamWriter pout = null;
  185. try {
  186. pout = new OutputStreamWriter(new FileOutputStream(portletXmlFile),
  187. Charset.forName("UTF-8"));
  188. } catch (FileNotFoundException e) {
  189. System.out.println(portletXmlFile + " not found!");
  190. }
  191. // open liferay-portlet.xml
  192. File liferayPortletXmlFile = new File(args[0] + File.separatorChar
  193. + LIFERAY_PORTLET_XML_FILE);
  194. OutputStreamWriter lpout = null;
  195. try {
  196. lpout = new OutputStreamWriter(new FileOutputStream(
  197. liferayPortletXmlFile), Charset.forName("UTF-8"));
  198. } catch (FileNotFoundException e) {
  199. System.out.println(liferayPortletXmlFile + " not found!");
  200. }
  201. // open liferay-display.xml
  202. File liferayDisplayXmlFile = new File(args[0] + File.separatorChar
  203. + LIFERAY_DISPLAY_XML_FILE);
  204. OutputStreamWriter ldout = null;
  205. try {
  206. ldout = new OutputStreamWriter(new FileOutputStream(
  207. liferayDisplayXmlFile), Charset.forName("UTF-8"));
  208. } catch (FileNotFoundException e) {
  209. System.out.println(liferayDisplayXmlFile + " not found!");
  210. }
  211. // open jboss object.xml
  212. File jbossObjectXmlFile = new File(args[0] + File.separatorChar
  213. + JBOSS_OBJECT_FILE);
  214. OutputStreamWriter joout = null;
  215. try {
  216. joout = new OutputStreamWriter(new FileOutputStream(
  217. jbossObjectXmlFile), Charset.forName("UTF-8"));
  218. } catch (FileNotFoundException e) {
  219. System.out.println(jbossObjectXmlFile + " not found!");
  220. }
  221. // open jboss instance.xml
  222. File jbossInstanceXmlFile = new File(args[0] + File.separatorChar
  223. + JBOSS_INSTANCE_FILE);
  224. OutputStreamWriter jiout = null;
  225. try {
  226. jiout = new OutputStreamWriter(new FileOutputStream(
  227. jbossInstanceXmlFile), Charset.forName("UTF-8"));
  228. } catch (FileNotFoundException e) {
  229. System.out.println(jbossInstanceXmlFile + " not found!");
  230. }
  231. if (pout != null && lpout != null && ldout != null && joout != null
  232. && jiout != null) {
  233. String pstring = PORTLET_XML_HEAD;
  234. String lpstring = LIFERAY_PORTLET_XML_HEAD;
  235. String ldstring = LIFERAY_DISPLAY_XML_HEAD;
  236. String jostring = JBOSS_OBJECT_HEAD;
  237. String jistring = JBOSS_INSTANCE_HEAD;
  238. Pattern p1 = Pattern
  239. .compile("<servlet-mapping>.*?<servlet-name>(.*?)<\\/servlet-name>.*?<url-pattern>(.*?)<\\/url-pattern>(.*?)<\\/servlet-mapping>");
  240. Pattern p2 = Pattern
  241. .compile(".*?<!--\\s+portlet\\s?style=([^ ]*)?\\s+-->.*?");
  242. Pattern findWidgetset = Pattern
  243. .compile("<init-param>.*?<param-name>widgetset<\\/param-name>.*?<param-value>(.*?)<\\/param-value>");
  244. Matcher m = p1.matcher(webXml);
  245. while (m.find()) {
  246. if (m.groupCount() < 3) {
  247. // don't include
  248. continue;
  249. }
  250. String name = m.group(1);
  251. // remove leading- and trailing whitespace
  252. name = name.replaceAll("^\\s*", "");
  253. name = name.replaceAll("\\s*$", "");
  254. String comment = m.group(3);
  255. Matcher m2 = p2.matcher(comment);
  256. if (!m2.find()) {
  257. // don't include
  258. continue;
  259. }
  260. String style = "";
  261. if (m2.groupCount() == 1 && m2.group(1) != null
  262. && !m2.group(1).equals("")) {
  263. style = "<init-param><name>style</name><value>"
  264. + m2.group(1) + "</value></init-param>";
  265. }
  266. // Find widgetset
  267. Pattern findServlet = Pattern
  268. .compile("<servlet>.*?<servlet-name>" + name
  269. + "<\\/servlet-name>(.*?)<\\/servlet>");
  270. Matcher servletMatcher = findServlet.matcher(webXml);
  271. if (servletMatcher.find()) {
  272. String servletXml = servletMatcher.group(1);
  273. Matcher widgetsetMatcher = findWidgetset
  274. .matcher(servletXml);
  275. if (widgetsetMatcher.find()) {
  276. String definedWidgetSet = widgetsetMatcher.group(1);
  277. if (!definedWidgetSet.equals(widgetset)) {
  278. System.err
  279. .println("WARNING: Widgetset in web.xml ("
  280. + definedWidgetSet
  281. + ") does not match used ("
  282. + widgetset + ")");
  283. }
  284. }
  285. }
  286. if (widgetset != null && !widgetset.equals("")) {
  287. System.err.println("Using widgetset: " + widgetset);
  288. style += "\n "
  289. + "<init-param><name>widgetset</name><value>"
  290. + widgetset + "</value></init-param>";
  291. }
  292. String pname = name + "Portlet";
  293. String url = m.group(2);
  294. // remove leading- and trailing whitespace
  295. url = url.replaceAll("^\\s*", "");
  296. url = url.replaceAll("\\s*$", "");
  297. if (url.startsWith("/")) {
  298. url = url.substring(1);
  299. }
  300. if (url.endsWith("*")) {
  301. url = url.substring(0, url.length() - 1);
  302. }
  303. if (url.endsWith("/")) {
  304. url = url.substring(0, url.length() - 1);
  305. }
  306. System.out.println("Mapping " + pname + " to " + url);
  307. String s = PORTLET_XML_SECTION;
  308. s = s.replaceAll("%NAME%", name);
  309. s = s.replaceAll("%PORTLETNAME%", pname);
  310. s = s.replaceAll("%URL%", url);
  311. s = s.replaceAll("%EXTRAPARAMS%", style);
  312. pstring += s;
  313. s = LIFERAY_PORTLET_XML_SECTION;
  314. s = s.replaceAll("%NAME%", name);
  315. s = s.replaceAll("%PORTLETNAME%", pname);
  316. s = s.replaceAll("%URL%", url);
  317. lpstring += s;
  318. s = LIFERAY_DISPLAY_XML_SECTION;
  319. s = s.replaceAll("%NAME%", name);
  320. s = s.replaceAll("%PORTLETNAME%", pname);
  321. s = s.replaceAll("%URL%", url);
  322. ldstring += s;
  323. s = JBOSS_OBJECT_SECTION;
  324. s = s.replaceAll("%NAME%", name);
  325. s = s.replaceAll("%PORTLETNAME%", pname);
  326. s = s.replaceAll("%URL%", url);
  327. jostring += s;
  328. s = JBOSS_INSTANCE_SECTION;
  329. s = s.replaceAll("%NAME%", name);
  330. s = s.replaceAll("%PORTLETNAME%", pname);
  331. s = s.replaceAll("%URL%", url);
  332. jistring += s;
  333. }
  334. pstring += PORTLET_XML_FOOT;
  335. lpstring += LIFERAY_PORTLET_XML_FOOT;
  336. ldstring += LIFERAY_DISPLAY_XML_FOOT;
  337. jostring += JBOSS_OBJECT_FOOT;
  338. jistring += JBOSS_INSTANCE_FOOT;
  339. try {
  340. pout.write(pstring);
  341. lpout.write(lpstring);
  342. ldout.write(ldstring);
  343. joout.write(jostring);
  344. jiout.write(jistring);
  345. } catch (IOException e) {
  346. System.out.println("Write FAILED:" + e);
  347. }
  348. }
  349. try {
  350. if (pout != null) {
  351. pout.close();
  352. }
  353. if (lpout != null) {
  354. lpout.close();
  355. }
  356. if (ldout != null) {
  357. ldout.close();
  358. }
  359. if (joout != null) {
  360. joout.close();
  361. }
  362. if (jiout != null) {
  363. jiout.close();
  364. }
  365. } catch (IOException e) {
  366. System.out.println("Close FAILED: " + e);
  367. }
  368. System.out.println("Done.");
  369. }
  370. }