Author: julien_viet
Date: 2010-07-23 05:17:09 -0400 (Fri, 23 Jul 2010)
New Revision: 3694
Added:
portal/trunk/component/resources/src/main/java/org/exoplatform/web/
portal/trunk/component/resources/src/main/java/org/exoplatform/web/application/
portal/trunk/component/resources/src/main/java/org/exoplatform/web/application/javascript/
portal/trunk/component/resources/src/main/java/org/exoplatform/web/application/javascript/JSMin.java
portal/trunk/component/resources/src/main/java/org/exoplatform/web/application/javascript/Javascript.java
portal/trunk/component/resources/src/main/java/org/exoplatform/web/application/javascript/JavascriptConfigParser.java
portal/trunk/component/resources/src/main/java/org/exoplatform/web/application/javascript/JavascriptConfigService.java
portal/trunk/component/resources/src/main/java/org/exoplatform/web/application/javascript/JavascriptDeployer.java
portal/trunk/component/resources/src/main/java/org/exoplatform/web/application/javascript/JavascriptKey.java
portal/trunk/component/resources/src/main/java/org/exoplatform/web/application/javascript/JavascriptRemoval.java
portal/trunk/component/resources/src/main/java/org/exoplatform/web/application/javascript/JavascriptTask.java
Removed:
portal/trunk/component/web/src/main/java/org/exoplatform/web/application/javascript/
portal/trunk/component/web/src/main/java/org/exoplatform/web/resource/config/xml/GateinResource.java
Log:
moving javascript service to component.module
Added:
portal/trunk/component/resources/src/main/java/org/exoplatform/web/application/javascript/JSMin.java
===================================================================
---
portal/trunk/component/resources/src/main/java/org/exoplatform/web/application/javascript/JSMin.java
(rev 0)
+++
portal/trunk/component/resources/src/main/java/org/exoplatform/web/application/javascript/JSMin.java 2010-07-23
09:17:09 UTC (rev 3694)
@@ -0,0 +1,342 @@
+/**
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+
+package org.exoplatform.web.application.javascript;
+
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.PushbackInputStream;
+
+public class JSMin
+{
+ private static final int EOF = -1;
+
+ private PushbackInputStream in;
+
+ private OutputStream out;
+
+ private int theA;
+
+ private int theB;
+
+ public JSMin(InputStream in, OutputStream out)
+ {
+ this.in = new PushbackInputStream(in);
+ this.out = out;
+ }
+
+ /**
+ * isAlphanum -- return true if the character is a letter, digit, underscore,
+ * dollar sign, or non-ASCII character.
+ */
+ static boolean isAlphanum(int c)
+ {
+ return ((c >= 'a' && c <= 'z') || (c >=
'0' && c <= '9') || (c >= 'A' && c <=
'Z') || c == '_' || c == '$'
+ || c == '\\' || c > 126);
+ }
+
+ /**
+ * get -- return the next character from stdin. Watch out for lookahead. If
+ * the character is a control character, translate it to a space or linefeed.
+ */
+ int get() throws IOException
+ {
+ int c = in.read();
+
+ if (c >= ' ' || c == '\n' || c == EOF)
+ {
+ return c;
+ }
+
+ if (c == '\r')
+ {
+ return '\n';
+ }
+
+ return ' ';
+ }
+
+ /**
+ * Get the next character without getting it.
+ */
+ int peek() throws IOException
+ {
+ int lookaheadChar = in.read();
+ in.unread(lookaheadChar);
+ return lookaheadChar;
+ }
+
+ /**
+ * next -- get the next character, excluding comments. peek() is used to see
+ * if a '/' is followed by a '/' or '*'.
+ */
+ int next() throws IOException, UnterminatedCommentException
+ {
+ int c = get();
+ if (c == '/')
+ {
+ switch (peek())
+ {
+ case '/' :
+ for (;;)
+ {
+ c = get();
+ if (c <= '\n')
+ {
+ return c;
+ }
+ }
+
+ case '*' :
+ get();
+ for (;;)
+ {
+ switch (get())
+ {
+ case '*' :
+ if (peek() == '/')
+ {
+ get();
+ return ' ';
+ }
+ break;
+ case EOF :
+ throw new UnterminatedCommentException();
+ }
+ }
+
+ default :
+ return c;
+ }
+
+ }
+ return c;
+ }
+
+ /**
+ * action -- do something! What you do is determined by the argument: 1 Output
+ * A. Copy B to A. Get the next B. 2 Copy B to A. Get the next B. (Delete A).
+ * 3 Get the next B. (Delete B). action treats a string as a single character.
+ * Wow! action recognizes a regular expression if it is preceded by ( or , or =.
+ */
+
+ void action(int d) throws IOException, UnterminatedRegExpLiteralException,
UnterminatedCommentException,
+ UnterminatedStringLiteralException
+ {
+ switch (d)
+ {
+ case 1 :
+ out.write(theA);
+ case 2 :
+ theA = theB;
+
+ if (theA == '\'' || theA == '"')
+ {
+ for (;;)
+ {
+ out.write(theA);
+ theA = get();
+ if (theA == theB)
+ {
+ break;
+ }
+ if (theA <= '\n')
+ {
+ throw new UnterminatedStringLiteralException();
+ }
+ if (theA == '\\')
+ {
+ out.write(theA);
+ theA = get();
+ }
+ }
+ }
+
+ case 3 :
+ theB = next();
+ if (theB == '/'
+ && (theA == '(' || theA == ',' || theA ==
'=' || theA == ':' || theA == '[' || theA == '!'
+ || theA == '&' || theA == '|' || theA ==
'?' || theA == '{' || theA == '}' || theA == ';' || theA
== '\n'))
+ {
+ out.write(theA);
+ out.write(theB);
+ for (;;)
+ {
+ theA = get();
+ if (theA == '/')
+ {
+ break;
+ }
+ else if (theA == '\\')
+ {
+ out.write(theA);
+ theA = get();
+ }
+ else if (theA <= '\n')
+ {
+ throw new UnterminatedRegExpLiteralException();
+ }
+ out.write(theA);
+ }
+ theB = next();
+ }
+ }
+ }
+
+ /**
+ * jsmin -- Copy the input to the output, deleting the characters which are
+ * insignificant to JavaScript. Comments will be removed. Tabs will be
+ * replaced with spaces. Carriage returns will be replaced with linefeeds.
+ * Most spaces and linefeeds will be removed.
+ */
+ public void jsmin() throws IOException, UnterminatedRegExpLiteralException,
UnterminatedCommentException,
+ UnterminatedStringLiteralException
+ {
+ theA = '\n';
+ action(3);
+ while (theA != EOF)
+ {
+ switch (theA)
+ {
+ case ' ' :
+ if (isAlphanum(theB))
+ {
+ action(1);
+ }
+ else
+ {
+ action(2);
+ }
+ break;
+ case '\n' :
+ switch (theB)
+ {
+ case '{' :
+ case '[' :
+ case '(' :
+ case '+' :
+ case '-' :
+ action(1);
+ break;
+ case ' ' :
+ action(3);
+ break;
+ default :
+ if (isAlphanum(theB))
+ {
+ action(1);
+ }
+ else
+ {
+ action(2);
+ }
+ }
+ break;
+ default :
+ switch (theB)
+ {
+ case ' ' :
+ if (isAlphanum(theA))
+ {
+ action(1);
+ break;
+ }
+ action(3);
+ break;
+ case '\n' :
+ switch (theA)
+ {
+ case '}' :
+ case ']' :
+ case ')' :
+ case '+' :
+ case '-' :
+ case '"' :
+ case '\'' :
+ action(1);
+ break;
+ default :
+ if (isAlphanum(theA))
+ {
+ action(1);
+ }
+ else
+ {
+ action(3);
+ }
+ }
+ break;
+ default :
+ action(1);
+ break;
+ }
+ }
+ }
+ out.flush();
+ }
+
+ class UnterminatedCommentException extends Exception
+ {
+ }
+
+ class UnterminatedStringLiteralException extends Exception
+ {
+ }
+
+ class UnterminatedRegExpLiteralException extends Exception
+ {
+ }
+
+ public static void main(String arg[])
+ {
+ try
+ {
+ JSMin jsmin = new JSMin(new FileInputStream(arg[0]), System.out);
+ jsmin.jsmin();
+ }
+ catch (FileNotFoundException e)
+ {
+ e.printStackTrace();
+ }
+ catch (ArrayIndexOutOfBoundsException e)
+ {
+ e.printStackTrace();
+ }
+ catch (IOException e)
+ {
+ e.printStackTrace();
+ }
+ catch (UnterminatedRegExpLiteralException e)
+ {
+ e.printStackTrace();
+ }
+ catch (UnterminatedCommentException e)
+ {
+ e.printStackTrace();
+ }
+ catch (UnterminatedStringLiteralException e)
+ {
+ e.printStackTrace();
+ }
+ }
+
+}
Added:
portal/trunk/component/resources/src/main/java/org/exoplatform/web/application/javascript/Javascript.java
===================================================================
---
portal/trunk/component/resources/src/main/java/org/exoplatform/web/application/javascript/Javascript.java
(rev 0)
+++
portal/trunk/component/resources/src/main/java/org/exoplatform/web/application/javascript/Javascript.java 2010-07-23
09:17:09 UTC (rev 3694)
@@ -0,0 +1,72 @@
+/*
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+
+package org.exoplatform.web.application.javascript;
+
+import javax.servlet.ServletContext;
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
+
+/**
+ * @author <a href="mailto:julien.viet@exoplatform.com">Julien
Viet</a>
+ * @version $Revision$
+ */
+public class Javascript
+{
+
+ /** . */
+ private final JavascriptKey key;
+
+ /** . */
+ private final ServletContext context;
+
+ /** . */
+ private final int priority;
+
+ public Javascript(JavascriptKey key, ServletContext context, Integer priority)
+ {
+ this.key = key;
+ this.context = context;
+ this.priority = priority != null ? priority : -1;
+ }
+
+ public String getPath() {
+ return key.getContextPath() + key.getScriptPath();
+ }
+
+ public JavascriptKey getKey()
+ {
+ return key;
+ }
+
+ public ServletContext getContext()
+ {
+ return context;
+ }
+
+ public int getPriority()
+ {
+ return priority;
+ }
+
+ public BufferedReader getReader()
+ {
+ return new BufferedReader(new
InputStreamReader(context.getResourceAsStream(key.getScriptPath())));
+ }
+}
Added:
portal/trunk/component/resources/src/main/java/org/exoplatform/web/application/javascript/JavascriptConfigParser.java
===================================================================
---
portal/trunk/component/resources/src/main/java/org/exoplatform/web/application/javascript/JavascriptConfigParser.java
(rev 0)
+++
portal/trunk/component/resources/src/main/java/org/exoplatform/web/application/javascript/JavascriptConfigParser.java 2010-07-23
09:17:09 UTC (rev 3694)
@@ -0,0 +1,144 @@
+/*
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.exoplatform.web.application.javascript;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.NodeList;
+
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.servlet.ServletContext;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+
+/**
+ * @author <a href="mailto:hoang281283@gmail.com">Minh Hoang
TO</a>
+ * @version $Id$
+ *
+ */
+public class JavascriptConfigParser
+{
+
+ final public static String JAVA_SCRIPT_TAG = "javascript";
+
+ final public static String JAVA_SCRIPT_PARAM = "param";
+
+ final public static String JAVA_SCRIPT_MODULE = "js-module";
+
+ final public static String JAVA_SCRIPT_PATH = "js-path";
+
+ final public static String JAVA_SCRIPT_PRIORITY = "js-priority";
+
+ /** . */
+ private ServletContext context;
+
+ private JavascriptConfigParser(ServletContext context)
+ {
+ this.context = context;
+ }
+
+ public static void processConfigResource(InputStream is, JavascriptConfigService
service, ServletContext scontext)
+ {
+ JavascriptConfigParser parser = new JavascriptConfigParser(scontext);
+ List<JavascriptTask> tasks = parser.fetchTasks(is);
+ if (tasks != null)
+ {
+ for (JavascriptTask task : tasks)
+ {
+ task.execute(service, scontext);
+ }
+ }
+ }
+
+ private List<JavascriptTask> fetchTasks(InputStream is)
+ {
+ try
+ {
+ DocumentBuilder docBuilder =
DocumentBuilderFactory.newInstance().newDocumentBuilder();
+ Document document = docBuilder.parse(is);
+ return fetchTasksFromXMLConfig(document);
+ }
+ catch (Exception ex)
+ {
+ return null;
+ }
+ }
+
+ private List<JavascriptTask> fetchTasksFromXMLConfig(Document document)
+ {
+ List<JavascriptTask> tasks = new ArrayList<JavascriptTask>();
+ Element element = document.getDocumentElement();
+ NodeList nodes = element.getElementsByTagName(JAVA_SCRIPT_TAG);
+ int length = nodes.getLength();
+ for (int i = 0; i < length; i++)
+ {
+ JavascriptTask task = xmlToTask((Element)nodes.item(i));
+ if (task != null)
+ {
+ tasks.add(task);
+ }
+ }
+ return tasks;
+ }
+
+ private JavascriptTask xmlToTask(Element element)
+ {
+ if (!JAVA_SCRIPT_TAG.equals(element.getTagName()))
+ {
+ return null;
+ }
+ try
+ {
+ JavascriptTask task = new JavascriptTask();
+ NodeList nodes = element.getElementsByTagName(JAVA_SCRIPT_PARAM);
+ int length = nodes.getLength();
+ for (int i = 0; i < length; i++)
+ {
+ Element param_ele = (Element)nodes.item(i);
+ String js_module =
+
param_ele.getElementsByTagName(JAVA_SCRIPT_MODULE).item(0).getFirstChild().getNodeValue();
+ String js_path =
+
param_ele.getElementsByTagName(JAVA_SCRIPT_PATH).item(0).getFirstChild().getNodeValue();
+ Integer js_priority = null;
+ try
+ {
+ js_priority =
+
Integer.valueOf(param_ele.getElementsByTagName(JAVA_SCRIPT_PRIORITY).item(0)
+ .getFirstChild().getNodeValue());
+ }
+ catch (Exception e)
+ {
+ //Js_priority still is null;
+ }
+ JavascriptKey key = new JavascriptKey(js_module, js_path,
context.getContextPath());
+ Javascript js = new Javascript(key, context, js_priority);
+ task.addScript(js);
+ }
+ return task;
+ }
+ catch (Exception ex)
+ {
+ ex.printStackTrace();
+ return null;
+ }
+ }
+}
Added:
portal/trunk/component/resources/src/main/java/org/exoplatform/web/application/javascript/JavascriptConfigService.java
===================================================================
---
portal/trunk/component/resources/src/main/java/org/exoplatform/web/application/javascript/JavascriptConfigService.java
(rev 0)
+++
portal/trunk/component/resources/src/main/java/org/exoplatform/web/application/javascript/JavascriptConfigService.java 2010-07-23
09:17:09 UTC (rev 3694)
@@ -0,0 +1,356 @@
+/**
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+
+package org.exoplatform.web.application.javascript;
+
+import org.exoplatform.commons.utils.Safe;
+import org.exoplatform.container.ExoContainerContext;
+import org.gatein.common.logging.Logger;
+import org.gatein.common.logging.LoggerFactory;
+import org.gatein.wci.impl.DefaultServletContainerFactory;
+import org.picocontainer.Startable;
+
+import java.io.*;
+import java.util.*;
+
+import javax.servlet.ServletContext;
+
+public class JavascriptConfigService implements Startable
+{
+
+ /** Our logger. */
+ private final Logger log = LoggerFactory.getLogger(JavascriptConfigService.class);
+
+ private Collection<String> availableScripts_;
+
+ private Collection<String> availableScriptsPaths_;
+
+ private List<Javascript> availableScriptsKey_;
+
+ private String mergedJavascript = "";
+
+ private HashMap<String, String> extendedJavascripts;
+
+ private byte[] jsBytes = null;
+
+ /** . */
+ private JavascriptDeployer deployer;
+
+ private JavascriptRemoval removal;
+
+ /** Used to clear merged Javascript on undeploying an webapp */
+ private Map<String, List<String>> object_view_of_merged_JS;
+
+ public JavascriptConfigService(ExoContainerContext context)
+ {
+ availableScripts_ = new ArrayList<String>();
+ availableScriptsPaths_ = new ArrayList<String>();
+ availableScriptsKey_ = new ArrayList<Javascript>();
+ extendedJavascripts = new HashMap<String, String>();
+ deployer = new JavascriptDeployer(context.getPortalContainerName(), this);
+ removal = new JavascriptRemoval(context.getPortalContainerName(), this);
+ object_view_of_merged_JS = new HashMap<String, List<String>>();
+ }
+
+ /**
+ * Return a collection list This method should return the availables scripts in the
service
+ * @return
+ */
+ public Collection<String> getAvailableScripts()
+ {
+ return availableScripts_;
+ }
+
+ /**
+ * Get a available script paths
+ * @return a collection list. This method should return the available script paths
+ */
+ public Collection<String> getAvailableScriptsPaths()
+ {
+ return availableScriptsPaths_;
+ }
+
+ /**
+ * Add extended JavaScript into available JavaScript
+ * @param module
+ * module name
+ * @param scriptPath
+ * URI path of JavaScript
+ * @param scontext
+ * the webapp's {@link javax.servlet.ServletContext}
+ * @param scriptData
+ * Content of JavaScript that will be added into available JavaScript
+ */
+ public synchronized void addExtendedJavascript(String module, String scriptPath,
ServletContext scontext, String scriptData)
+ {
+ String servletContextName = scontext.getServletContextName();
+ String path = "/" + servletContextName + scriptPath;
+ availableScripts_.add(module);
+ availableScriptsPaths_.add(path);
+ extendedJavascripts.put(path, scriptData);
+ }
+
+ /**
+ * Clear available JavaScript and add new JavaScripts
+ * @param jsKeys
+ * new list of JavaScript will replace current available JavaScript
+ */
+ @SuppressWarnings("unchecked")
+ public synchronized void addJavascripts(List<Javascript> jsKeys)
+ {
+ availableScriptsKey_.addAll(jsKeys);
+
+
+ Collections.sort(availableScriptsKey_, new Comparator<Javascript>()
+ {
+ public int compare(Javascript o1, Javascript o2)
+ {
+ if (o1.getPriority() == o2.getPriority())
+ return o1.getKey().getModule().compareTo(o2.getKey().getModule());
+ else if (o1.getPriority() < 0)
+ return 1;
+ else if (o2.getPriority() < 0)
+ return -1;
+ else
+ return o1.getPriority() - o2.getPriority();
+ }
+ });
+
+ mergedJavascript = "";
+ availableScripts_.clear();
+ availableScriptsPaths_.clear();
+ object_view_of_merged_JS.clear();
+
+ //
+ for (Javascript script : availableScriptsKey_) {
+ addJavascript(script);
+ }
+ }
+
+ /**
+ * Add an JavaScript into available JavaScripts
+ * @param javascript
+ * JavaScript will be added into available JavaScript
+ */
+ private void addJavascript(Javascript javascript)
+ {
+ availableScripts_.add(javascript.getKey().getModule());
+ availableScriptsPaths_.add(javascript.getPath());
+
+ List<String> mergedJS_list =
object_view_of_merged_JS.get(javascript.getKey().getContextPath());
+ if (mergedJS_list == null)
+ {
+ mergedJS_list = new ArrayList<String>();
+ object_view_of_merged_JS.put(javascript.getKey().getContextPath(),
mergedJS_list);
+ }
+
+ StringBuffer sB = new StringBuffer();
+ String line = "";
+ try
+ {
+ BufferedReader reader = javascript.getReader();
+ try
+ {
+ while ((line = reader.readLine()) != null)
+ {
+ line = line + "\n";
+ sB.append(line);
+ mergedJS_list.add(line);
+ }
+ }
+ catch (Exception ex)
+ {
+ ex.printStackTrace();
+ }
+ finally
+ {
+ Safe.close(reader);
+ }
+ }
+ catch (Exception e)
+ {
+ e.printStackTrace();
+ }
+ sB.append("\n");
+ mergedJS_list.add("\n");
+
+ mergedJavascript = mergedJavascript.concat(sB.toString());
+ }
+
+ /**
+ * Remove JavaScripts from availabe JavaScipts by ServletContext
+ * @param scontext
+ * the webapp's {@link javax.servlet.ServletContext}
+ *
+ */
+ public synchronized void remove(ServletContext context)
+ {
+ // We clone to avoid concurrent modification exception
+ for (Javascript script : new ArrayList<Javascript>(availableScriptsKey_))
+ {
+ if (script.getContext().getContextPath().equals(context.getContextPath())) {
+ removeJavascript(script);
+ }
+ }
+ }
+
+ /**
+ * Remove JavaScript from available JavaScripts
+ * @param script
+ * JavaScript will be removed
+ */
+ public synchronized void removeJavascript(Javascript script)
+ {
+ availableScripts_.remove(script.getKey().getModule());
+ availableScriptsPaths_.remove(script.getPath());
+ object_view_of_merged_JS.remove(script.getKey().getContextPath());
+
+ // Enlist entries to be removed
+ for (Iterator<Javascript> i = availableScriptsKey_.iterator();i.hasNext();)
+ {
+ Javascript _script = i.next();
+ if (script.getKey().equals(_script.getKey()))
+ {
+ i.remove();
+ }
+ }
+ }
+
+ /**
+ * Refresh the mergedJavascript
+ */
+ public void refreshMergedJavascript()
+ {
+ mergedJavascript = "";
+ StringBuffer buffer = new StringBuffer();
+ for (String webApp : object_view_of_merged_JS.keySet())
+ {
+ for (String jsPath : object_view_of_merged_JS.get(webApp))
+ {
+ buffer.append(jsPath);
+ }
+ }
+ mergedJavascript = buffer.toString();
+ }
+
+ /**
+ * Write the merged javascript in a provided output stream.
+ *
+ * @param out the output stream
+ * @throws IOException any io exception
+ */
+ public void writeMergedJavascript(OutputStream out) throws IOException
+ {
+ if (jsBytes == null)
+ {
+ // Generate javascript in a buffer
+ StringBuffer allJavascript = new StringBuffer();
+ allJavascript.append(mergedJavascript);
+ for (String script : extendedJavascripts.values())
+ {
+ allJavascript.append(script);
+ }
+ String s = allJavascript.toString();
+
+ // Get bytes
+ byte[] bytes;
+ try
+ {
+ bytes = s.getBytes("UTF-8");
+ }
+ catch (UnsupportedEncodingException e)
+ {
+ throw new AssertionError("No access to UTF-8, e");
+ }
+
+ // Minify
+ try
+ {
+ ByteArrayInputStream input = new ByteArrayInputStream(bytes);
+ ByteArrayOutputStream jsStream = new ByteArrayOutputStream();
+ JSMin jsMin = new JSMin(input, jsStream);
+ jsMin.jsmin();
+ jsBytes = jsStream.toByteArray();
+ }
+ catch (Exception e)
+ {
+ log.error("Error when generating minified javascript, will use normal
javascript instead", e);
+ jsBytes = bytes;
+ }
+ }
+
+ //
+ out.write(jsBytes);
+ }
+
+ /**
+ * Check the existence of module in Available Scripts
+ * @param module
+ * @return true if Available Scripts contain module, else return false
+ */
+ public boolean isModuleLoaded(CharSequence module)
+ {
+ return getAvailableScripts().contains(module);
+ }
+
+ /**
+ * Remove JavaScript from available JavaScripts and extended JavaScripts
+ * @param module
+ * module will be removed
+ * @param scriptPath
+ * URI of script that will be removed
+ * @param scontext
+ * the webapp's {@link javax.servlet.ServletContext}
+ *
+ */
+ public void removeExtendedJavascript(String module, String scriptPath, ServletContext
scontext)
+ {
+ String servletContextName = scontext.getServletContextName();
+ availableScripts_.remove(module);
+ String path = "/" + servletContextName + scriptPath;
+ availableScriptsPaths_.remove(path);
+ extendedJavascripts.remove(path);
+ jsBytes = null;
+ }
+
+ /**
+ * Start service.
+ * Registry org.exoplatform.web.application.javascript.JavascriptDeployer,
+ * org.exoplatform.web.application.javascript.JavascriptRemoval into
ServletContainer
+ * @see org.picocontainer.Startable#start()
+ */
+ public void start()
+ {
+
DefaultServletContainerFactory.getInstance().getServletContainer().addWebAppListener(deployer);
+
DefaultServletContainerFactory.getInstance().getServletContainer().addWebAppListener(removal);
+ }
+
+ /**
+ * Stop service.
+ * Remove org.exoplatform.web.application.javascript.JavascriptDeployer,
+ * org.exoplatform.web.application.javascript.JavascriptRemoval from
ServletContainer
+ * @see org.picocontainer.Startable#stop()
+ */
+ public void stop()
+ {
+
DefaultServletContainerFactory.getInstance().getServletContainer().removeWebAppListener(deployer);
+
DefaultServletContainerFactory.getInstance().getServletContainer().removeWebAppListener(removal);
+ }
+
+}
\ No newline at end of file
Added:
portal/trunk/component/resources/src/main/java/org/exoplatform/web/application/javascript/JavascriptDeployer.java
===================================================================
---
portal/trunk/component/resources/src/main/java/org/exoplatform/web/application/javascript/JavascriptDeployer.java
(rev 0)
+++
portal/trunk/component/resources/src/main/java/org/exoplatform/web/application/javascript/JavascriptDeployer.java 2010-07-23
09:17:09 UTC (rev 3694)
@@ -0,0 +1,146 @@
+/**
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+
+package org.exoplatform.web.application.javascript;
+
+import org.exoplatform.container.PortalContainer;
+import org.exoplatform.container.RootContainer.PortalContainerPostInitTask;
+import org.exoplatform.services.log.ExoLogger;
+import org.exoplatform.services.log.Log;
+import org.gatein.wci.WebAppEvent;
+import org.gatein.wci.WebAppLifeCycleEvent;
+import org.gatein.wci.WebAppListener;
+import org.gatein.wci.impl.DefaultServletContainerFactory;
+import org.picocontainer.Startable;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+import javax.servlet.ServletContext;
+
+/**
+ * Created by The eXo Platform SAS
+ * Jan 19, 2007
+ */
+
+public class JavascriptDeployer implements WebAppListener, Startable
+{
+
+ private static final String GATEIN_CONFIG_RESOURCE =
"/WEB-INF/gatein-resources.xml";
+
+ /**
+ * Logger
+ */
+ private static final Log LOG = ExoLogger.getLogger(JavascriptDeployer.class);
+
+ /** . */
+ private final JavascriptConfigService javascriptService;
+
+ /**
+ * The name of the portal container
+ */
+ private final String portalContainerName;
+
+ public JavascriptDeployer(String portalContainerName, JavascriptConfigService
javascriptService)
+ {
+ this.javascriptService = javascriptService;
+ this.portalContainerName = portalContainerName;
+ }
+
+ public void start()
+ {
+
DefaultServletContainerFactory.getInstance().getServletContainer().addWebAppListener(this);
+ }
+
+ public void stop()
+ {
+
DefaultServletContainerFactory.getInstance().getServletContainer().removeWebAppListener(this);
+ }
+
+ public void onEvent(WebAppEvent event)
+ {
+ if (event instanceof WebAppLifeCycleEvent)
+ {
+ WebAppLifeCycleEvent waEvent = (WebAppLifeCycleEvent)event;
+ if (waEvent.getType() == WebAppLifeCycleEvent.ADDED)
+ {
+ ServletContext scontext = null;
+ try
+ {
+ scontext = event.getWebApp().getServletContext();
+
+ InputStream is = scontext.getResourceAsStream(GATEIN_CONFIG_RESOURCE);
+ if (is == null)
+ return;
+ try
+ {
+ is.close();
+ }
+ catch (Exception ex)
+ {
+ // ignore me
+ }
+ final PortalContainerPostInitTask task = new
PortalContainerPostInitTask()
+ {
+
+ public void execute(ServletContext scontext, PortalContainer
portalContainer)
+ {
+ register(scontext, portalContainer);
+ }
+ };
+ PortalContainer.addInitTask(scontext, task, portalContainerName);
+ }
+ catch (Exception ex)
+ {
+ LOG.error("An error occurs while registering 'Javascript in
gatein-resources.xml' from the context '"
+ + (scontext == null ? "unknown" :
scontext.getServletContextName()) + "'", ex);
+ }
+ }
+ }
+ }
+
+ private void register(ServletContext scontext, PortalContainer container)
+ {
+ InputStream is = null;
+ try
+ {
+ is = scontext.getResourceAsStream(GATEIN_CONFIG_RESOURCE);
+ JavascriptConfigParser.processConfigResource(is, javascriptService, scontext);
+ }
+ catch (Exception ex)
+ {
+ LOG.error("An error occurs while processing 'Javascript in
gatein-resources.xml' from the context '"
+ + scontext.getServletContextName() + "'", ex);
+ }
+ finally
+ {
+ if (is != null)
+ {
+ try
+ {
+ is.close();
+ }
+ catch (IOException e)
+ {
+ // ignore me
+ }
+ }
+ }
+ }
+}
\ No newline at end of file
Added:
portal/trunk/component/resources/src/main/java/org/exoplatform/web/application/javascript/JavascriptKey.java
===================================================================
---
portal/trunk/component/resources/src/main/java/org/exoplatform/web/application/javascript/JavascriptKey.java
(rev 0)
+++
portal/trunk/component/resources/src/main/java/org/exoplatform/web/application/javascript/JavascriptKey.java 2010-07-23
09:17:09 UTC (rev 3694)
@@ -0,0 +1,78 @@
+/*
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.exoplatform.web.application.javascript;
+
+/**
+ * @author <a href="mailto:hoang281283@gmail.com">Minh Hoang
TO</a>
+ * @version $Id$
+ *
+ */
+public class JavascriptKey
+{
+
+ /** . */
+ private final String module;
+
+ /** . */
+ private final String scriptPath;
+
+ /** . */
+ private final String contextPath;
+
+ public JavascriptKey(String module, String scriptPath, String contextPath) throws
IllegalArgumentException
+ {
+ if (module == null || scriptPath == null || contextPath == null)
+ {
+ throw new IllegalArgumentException("Module and scriptPath are mandatory for
JavascriptKey");
+ }
+ this.module = module;
+ this.scriptPath = scriptPath;
+ this.contextPath = contextPath;
+ }
+
+ @Override
+ public boolean equals(Object obj)
+ {
+ if (obj == this)
+ {
+ return true;
+ }
+ if (obj instanceof JavascriptKey)
+ {
+ JavascriptKey that = (JavascriptKey)obj;
+ return module.equals(that.module) && scriptPath.equals(that.scriptPath)
&& contextPath.equals(that.contextPath);
+ }
+ return false;
+ }
+
+ public String getModule()
+ {
+ return module;
+ }
+
+ public String getScriptPath()
+ {
+ return scriptPath;
+ }
+
+ public String getContextPath()
+ {
+ return contextPath;
+ }
+}
Added:
portal/trunk/component/resources/src/main/java/org/exoplatform/web/application/javascript/JavascriptRemoval.java
===================================================================
---
portal/trunk/component/resources/src/main/java/org/exoplatform/web/application/javascript/JavascriptRemoval.java
(rev 0)
+++
portal/trunk/component/resources/src/main/java/org/exoplatform/web/application/javascript/JavascriptRemoval.java 2010-07-23
09:17:09 UTC (rev 3694)
@@ -0,0 +1,89 @@
+/*
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.exoplatform.web.application.javascript;
+
+import org.gatein.wci.WebAppEvent;
+import org.gatein.wci.WebAppLifeCycleEvent;
+import org.gatein.wci.WebAppListener;
+import org.gatein.wci.impl.DefaultServletContainerFactory;
+import org.picocontainer.Startable;
+
+import javax.servlet.ServletContext;
+
+/**
+ * @author <a href="mailto:hoang281283@gmail.com">Minh Hoang
TO</a>
+ * @version $Id$
+ *
+ */
+public class JavascriptRemoval implements WebAppListener, Startable
+{
+
+ private String portalContainerName;
+
+ private JavascriptConfigService javascriptService;
+
+ public JavascriptRemoval(String _portalContainerName, JavascriptConfigService
_javascriptService)
+ {
+ this.portalContainerName = _portalContainerName;
+ this.javascriptService = _javascriptService;
+ }
+
+ /**
+ * @see org.gatein.wci.WebAppListener#onEvent(org.gatein.wci.WebAppEvent)
+ */
+ public void onEvent(WebAppEvent arg0)
+ {
+ if (arg0 instanceof WebAppLifeCycleEvent)
+ {
+ WebAppLifeCycleEvent wevent = (WebAppLifeCycleEvent)arg0;
+ if (wevent.getType() == WebAppLifeCycleEvent.REMOVED)
+ {
+ removeJavascript(wevent.getWebApp().getServletContext());
+ refreshJavascript();
+ }
+ }
+ }
+
+ /**
+ * Removes javascript deployed in this web app.
+ *
+ * @param scontext the servlet context
+ */
+ private void removeJavascript(ServletContext scontext)
+ {
+ String webApp = scontext.getContextPath();
+ javascriptService.remove(scontext);
+ }
+
+ private void refreshJavascript()
+ {
+ javascriptService.refreshMergedJavascript();
+ }
+
+ public void start()
+ {
+
DefaultServletContainerFactory.getInstance().getServletContainer().addWebAppListener(this);
+ }
+
+ public void stop()
+ {
+
DefaultServletContainerFactory.getInstance().getServletContainer().removeWebAppListener(this);
+ }
+
+}
Added:
portal/trunk/component/resources/src/main/java/org/exoplatform/web/application/javascript/JavascriptTask.java
===================================================================
---
portal/trunk/component/resources/src/main/java/org/exoplatform/web/application/javascript/JavascriptTask.java
(rev 0)
+++
portal/trunk/component/resources/src/main/java/org/exoplatform/web/application/javascript/JavascriptTask.java 2010-07-23
09:17:09 UTC (rev 3694)
@@ -0,0 +1,50 @@
+/*
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.exoplatform.web.application.javascript;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.servlet.ServletContext;
+
+/**
+ * @author <a href="mailto:hoang281283@gmail.com">Minh Hoang
TO</a>
+ * @version $Id$
+ *
+ */
+public class JavascriptTask
+{
+
+ private List<Javascript> scripts;
+
+ public JavascriptTask()
+ {
+ scripts = new ArrayList<Javascript>();
+ }
+
+ public void execute(JavascriptConfigService service, ServletContext scontext)
+ {
+ service.addJavascripts(scripts);
+ }
+
+ public void addScript(Javascript script)
+ {
+ scripts.add(script);
+ }
+}
Deleted:
portal/trunk/component/web/src/main/java/org/exoplatform/web/resource/config/xml/GateinResource.java
===================================================================
---
portal/trunk/component/web/src/main/java/org/exoplatform/web/resource/config/xml/GateinResource.java 2010-07-23
08:40:15 UTC (rev 3693)
+++
portal/trunk/component/web/src/main/java/org/exoplatform/web/resource/config/xml/GateinResource.java 2010-07-23
09:17:09 UTC (rev 3694)
@@ -1,46 +0,0 @@
-/**
- * Copyright (C) 2009 eXo Platform SAS.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
- */
-
-package org.exoplatform.web.resource.config.xml;
-
-import org.w3c.dom.Element;
-
-/**
- *
- * Created by eXoPlatform SAS
- *
- * Author: Minh Hoang TO - hoang281283(a)gmail.com
- *
- * Sep 17, 2009
- */
-public interface GateinResource
-{
-
- final public static String JAVA_SCRIPT_TAG = "javascript";
-
- final public static String JAVA_SCRIPT_PARAM = "param";
-
- final public static String JAVA_SCRIPT_MODULE = "js-module";
-
- final public static String JAVA_SCRIPT_PATH = "js-path";
-
- final public static String JAVA_SCRIPT_PRIORITY = "js-priority";
-
- public void binding(Element elemt);
-}