You are viewing a plain text version of this content. The canonical link for it is here.
Posted to bridges-commits@portals.apache.org by wo...@apache.org on 2010/04/23 13:06:13 UTC
svn commit: r937248 - in /portals/bridges/bridges-script/trunk/portlet/src:
main/java/org/apache/portals/bridges/script/
test/java/org/apache/portals/bridges/script/
Author: woonsan
Date: Fri Apr 23 11:06:13 2010
New Revision: 937248
URL: http://svn.apache.org/viewvc?rev=937248&view=rev
Log:
PB-107: Adding refresh-delay option
Added:
portals/bridges/bridges-script/trunk/portlet/src/main/java/org/apache/portals/bridges/script/DelegatingScriptSourceAdaptor.java (with props)
portals/bridges/bridges-script/trunk/portlet/src/main/java/org/apache/portals/bridges/script/ScriptSource.java (with props)
portals/bridges/bridges-script/trunk/portlet/src/main/java/org/apache/portals/bridges/script/ScriptSourceFactory.java (with props)
portals/bridges/bridges-script/trunk/portlet/src/main/java/org/apache/portals/bridges/script/SimpleScriptSource.java (contents, props changed)
- copied, changed from r937175, portals/bridges/bridges-script/trunk/portlet/src/main/java/org/apache/portals/bridges/script/ScriptSource.java
portals/bridges/bridges-script/trunk/portlet/src/main/java/org/apache/portals/bridges/script/SimpleScriptSourceFactory.java (with props)
Modified:
portals/bridges/bridges-script/trunk/portlet/src/main/java/org/apache/portals/bridges/script/ScriptPortlet.java
portals/bridges/bridges-script/trunk/portlet/src/main/java/org/apache/portals/bridges/script/ScriptPortletPreferencesValidator.java
portals/bridges/bridges-script/trunk/portlet/src/test/java/org/apache/portals/bridges/script/AbstractSimpleScriptPortletTestCase.java
Added: portals/bridges/bridges-script/trunk/portlet/src/main/java/org/apache/portals/bridges/script/DelegatingScriptSourceAdaptor.java
URL: http://svn.apache.org/viewvc/portals/bridges/bridges-script/trunk/portlet/src/main/java/org/apache/portals/bridges/script/DelegatingScriptSourceAdaptor.java?rev=937248&view=auto
==============================================================================
--- portals/bridges/bridges-script/trunk/portlet/src/main/java/org/apache/portals/bridges/script/DelegatingScriptSourceAdaptor.java (added)
+++ portals/bridges/bridges-script/trunk/portlet/src/main/java/org/apache/portals/bridges/script/DelegatingScriptSourceAdaptor.java Fri Apr 23 11:06:13 2010
@@ -0,0 +1,79 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.portals.bridges.script;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+/**
+ * DelegatingScriptSourceAdaptor
+ * <P>
+ * Simple adaptor class implementing ScriptSource by delegating all to the delegatee
+ * </P>
+ *
+ * @author <a href="mailto:woonsan@apache.org">Woonsan Ko</a>
+ * @version $Id$
+ */
+public class DelegatingScriptSourceAdaptor implements ScriptSource
+{
+ private ScriptSource delegatee;
+
+ public DelegatingScriptSourceAdaptor(final ScriptSource delegatee)
+ {
+ if (delegatee == null)
+ {
+ throw new IllegalArgumentException("Delegatee cannot be null!");
+ }
+
+ this.delegatee = delegatee;
+ }
+
+ public String getName()
+ {
+ return delegatee.getName();
+ }
+
+ public String getExtension()
+ {
+ return delegatee.getExtension();
+ }
+
+ public String getCharacterEncoding()
+ {
+ return delegatee.getCharacterEncoding();
+ }
+
+ public void setMimeType(String mimeType)
+ {
+ delegatee.setMimeType(mimeType);
+ }
+
+ public String getMimeType()
+ {
+ return delegatee.getMimeType();
+ }
+
+ public long lastModified()
+ {
+ return delegatee.lastModified();
+ }
+
+ public InputStream getInputStream() throws IOException
+ {
+ return delegatee.getInputStream();
+ }
+}
Propchange: portals/bridges/bridges-script/trunk/portlet/src/main/java/org/apache/portals/bridges/script/DelegatingScriptSourceAdaptor.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: portals/bridges/bridges-script/trunk/portlet/src/main/java/org/apache/portals/bridges/script/DelegatingScriptSourceAdaptor.java
------------------------------------------------------------------------------
svn:keywords = Id
Propchange: portals/bridges/bridges-script/trunk/portlet/src/main/java/org/apache/portals/bridges/script/DelegatingScriptSourceAdaptor.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: portals/bridges/bridges-script/trunk/portlet/src/main/java/org/apache/portals/bridges/script/ScriptPortlet.java
URL: http://svn.apache.org/viewvc/portals/bridges/bridges-script/trunk/portlet/src/main/java/org/apache/portals/bridges/script/ScriptPortlet.java?rev=937248&r1=937247&r2=937248&view=diff
==============================================================================
--- portals/bridges/bridges-script/trunk/portlet/src/main/java/org/apache/portals/bridges/script/ScriptPortlet.java (original)
+++ portals/bridges/bridges-script/trunk/portlet/src/main/java/org/apache/portals/bridges/script/ScriptPortlet.java Fri Apr 23 11:06:13 2010
@@ -101,13 +101,21 @@ import javax.script.ScriptException;
* </init-param>
*
* <!-- Optional init parameter for auto-refresh option.
- * If auto-refresh is true, then a modification of script source applies instantly.
- * By default, this option is not set to true. -->
+ * If auto-refresh is true, then a modification of script source can be refreshed automatically.
+ * By default, this option is set to false. -->
* <init-param>
* <name>auto-refresh</name>
* <value>true</value>
* </init-param>
*
+ * <!-- Optional init parameter for refresh-delay option.
+ * When auto-refresh is true, this init parameter sets the milliseconds of automatic refreshing interval.
+ * By default, this option is set to 60000 milliseconds (a minute). -->
+ * <init-param>
+ * <name>refresh-delay</name>
+ * <value>60000</value>
+ * </init-param>
+ *
* <!-- Optional init parameter for script preferences validator path -->
* <init-param>
* <name>validator</name>
@@ -169,45 +177,57 @@ public class ScriptPortlet extends Gener
public static final String AUTO_REFRESH = "auto-refresh";
- protected PortletConfig portletConfig;
+ public static final String REFRESH_DELAY = "refresh-delay";
+
+ public static final String SCRIPT_SOURCE_FACTORY = "script-source-factory";
+
+ private PortletConfig portletConfig;
+
+ private String scriptEngineName;
+
+ private String evalKey;
+
+ private ScriptEngine scriptEngine;
- protected String scriptEngineName;
+ private String scriptSourceUri;
- protected String evalKey;
+ private String scriptSourceUriEncoding;
- protected ScriptEngine scriptEngine;
+ private String scriptSourceCharacterEncoding;
- protected String scriptSourceUri;
+ private boolean autoRefresh;
- protected String scriptSourceUriEncoding;
+ private long refreshDelay = 60000L;
- protected String scriptSourceCharacterEncoding;
+ private ScriptSource scriptSource;
- protected boolean autoRefresh;
+ private long scriptSourceLastEvalStarted;
- protected ScriptSource scriptSource;
+ private long scriptSourceLastEvaluated;
- protected long scriptSourceLastModified;
+ private long scriptSourceLastModified;
- protected long scriptSourceLastEvaluated;
+ private Portlet scriptPortletInstance;
- protected Portlet scriptPortletInstance;
+ private GenericPortlet scriptGenericPortletInstance;
- protected GenericPortlet scriptGenericPortletInstance;
+ private Method portletDoEditMethod;
- protected Method portletDoEditMethod;
+ private String validatorSourceUri;
- protected String validatorSourceUri;
+ private ScriptSource validatorSource;
- protected ScriptSource validatorSource;
+ private long validatorSourceLastEvalStarted;
- protected long validatorSourceLastModified;
+ private long validatorSourceLastEvaluated;
- protected long validatorSourceLastEvaluated;
+ private long validatorSourceLastModified;
- protected PreferencesValidator validatorInstance;
+ private PreferencesValidator validatorInstance;
- private static ThreadLocal<PreferencesValidator> tlValidatorInstance = new ThreadLocal<PreferencesValidator>();
+ private static ThreadLocal<PreferencesValidator> tlCurrentValidatorInstance = new ThreadLocal<PreferencesValidator>();
+
+ private ScriptSourceFactory scriptSourceFactory = new SimpleScriptSourceFactory();
public ScriptPortlet()
{
@@ -219,8 +239,6 @@ public class ScriptPortlet extends Gener
{
portletConfig = config;
- autoRefresh = "true".equals(config.getInitParameter(AUTO_REFRESH));
-
String param = config.getInitParameter(ENGINE);
if (param != null && !"".equals(param.trim()))
@@ -235,6 +253,29 @@ public class ScriptPortlet extends Gener
evalKey = param;
}
+ autoRefresh = "true".equals(config.getInitParameter(AUTO_REFRESH));
+
+ param = config.getInitParameter(REFRESH_DELAY);
+
+ if (param != null && !"".equals(param.trim()))
+ {
+ refreshDelay = Long.parseLong(param.trim());
+ }
+
+ param = config.getInitParameter(SCRIPT_SOURCE_FACTORY);
+
+ if (param != null && !"".equals(param.trim()))
+ {
+ try
+ {
+ scriptSourceFactory = (ScriptSourceFactory) Thread.currentThread().getContextClassLoader().loadClass(param).newInstance();
+ }
+ catch (Exception e)
+ {
+ throw new PortletException("Configuration failed: " + SCRIPT_SOURCE_FACTORY + ". Cannot create script source factory: " + param);
+ }
+ }
+
param = config.getInitParameter(ENCODING);
if (param != null && !"".equals(param.trim()))
@@ -261,11 +302,11 @@ public class ScriptPortlet extends Gener
{
if (scriptSourceUri.startsWith("/"))
{
- scriptSource = new ScriptSource(new File(config.getPortletContext().getRealPath(scriptSourceUri)).toURL().toString(), scriptSourceUriEncoding, scriptSourceCharacterEncoding);
+ scriptSource = scriptSourceFactory.createScriptSource(new File(config.getPortletContext().getRealPath(scriptSourceUri)).toURL().toString(), scriptSourceUriEncoding, scriptSourceCharacterEncoding);
}
else
{
- scriptSource = new ScriptSource(scriptSourceUri, scriptSourceUriEncoding, scriptSourceCharacterEncoding);
+ scriptSource = scriptSourceFactory.createScriptSource(scriptSourceUri, scriptSourceUriEncoding, scriptSourceCharacterEncoding);
}
scriptSource.setMimeType(config.getPortletContext().getMimeType(scriptSource.getName()));
@@ -291,11 +332,11 @@ public class ScriptPortlet extends Gener
{
if (validatorSourceUri.startsWith("/"))
{
- validatorSource = new ScriptSource(new File(config.getPortletContext().getRealPath(validatorSourceUri)).toURL().toString(), scriptSourceUriEncoding, scriptSourceCharacterEncoding);
+ validatorSource = scriptSourceFactory.createScriptSource(new File(config.getPortletContext().getRealPath(validatorSourceUri)).toURL().toString(), scriptSourceUriEncoding, scriptSourceCharacterEncoding);
}
else
{
- validatorSource = new ScriptSource(validatorSourceUri, scriptSourceUriEncoding, scriptSourceCharacterEncoding);
+ validatorSource = scriptSourceFactory.createScriptSource(validatorSourceUri, scriptSourceUriEncoding, scriptSourceCharacterEncoding);
}
validatorSource.setMimeType(config.getPortletContext().getMimeType(validatorSource.getName()));
@@ -355,7 +396,7 @@ public class ScriptPortlet extends Gener
if (validatorInstance != null)
{
- tlValidatorInstance.set(validatorInstance);
+ tlCurrentValidatorInstance.set(validatorInstance);
}
scriptPortletInstance.processAction(request, response);
@@ -364,7 +405,7 @@ public class ScriptPortlet extends Gener
{
if (validatorInstance != null)
{
- tlValidatorInstance.set(null);
+ tlCurrentValidatorInstance.set(null);
}
}
}
@@ -425,13 +466,25 @@ public class ScriptPortlet extends Gener
protected void refreshPortletInstance() throws PortletException
{
- boolean createNew = (scriptPortletInstance == null);
+ boolean scriptCreated = (scriptPortletInstance != null);
+ boolean scriptModified = false;
+ long checkedScriptSourceLastModified = 0L;
- if (!createNew)
+ if (scriptCreated)
{
- createNew = (autoRefresh && ScriptEngineUtils.isScriptModified(scriptSource, scriptSourceLastModified));
+ if (autoRefresh)
+ {
+ if ((refreshDelay <= 0L) || (System.currentTimeMillis() - scriptSourceLastEvalStarted > refreshDelay))
+ {
+ scriptModified = (ScriptEngineUtils.isScriptModified(scriptSource, scriptSourceLastModified));
+ }
+ }
- if (!createNew)
+ if (scriptModified)
+ {
+ checkedScriptSourceLastModified = scriptSource.lastModified();
+ }
+ else
{
return;
}
@@ -439,55 +492,62 @@ public class ScriptPortlet extends Gener
try
{
- Portlet tempScriptPortletInstance = null;
-
- if (scriptEngine == null)
+ synchronized (this)
{
- scriptEngine = ScriptEngineUtils.createScriptEngine(scriptEngineName, scriptSource);
- }
-
- Object evalPortlet = ScriptEngineUtils.evaluateScript(scriptEngine, scriptSource, evalKey, true);
- scriptSourceLastEvaluated = System.currentTimeMillis();
+ if ((scriptModified && scriptSourceLastEvaluated >= checkedScriptSourceLastModified) || (!scriptCreated && scriptPortletInstance != null))
+ {
+ return;
+ }
- if (evalPortlet instanceof Portlet)
- {
- tempScriptPortletInstance = (Portlet) evalPortlet;
- }
- else if (evalPortlet instanceof Class)
- {
- Class<? extends Portlet> scriptPortletClass = (Class<? extends Portlet>) evalPortlet;
- tempScriptPortletInstance = (Portlet) scriptPortletClass.newInstance();
- }
- else
- {
- throw new ScriptException("The evaluated return is neither class nor instance of javax.portlet.Portlet. " + evalPortlet);
- }
-
- scriptGenericPortletInstance = null;
- portletDoEditMethod = null;
-
- if (tempScriptPortletInstance instanceof GenericPortlet)
- {
- scriptGenericPortletInstance = (GenericPortlet) tempScriptPortletInstance;
+ scriptSourceLastModified = checkedScriptSourceLastModified;
+ scriptSourceLastEvalStarted = System.currentTimeMillis();
+ Portlet tempScriptPortletInstance = null;
+ if (scriptEngine == null)
+ {
+ scriptEngine = ScriptEngineUtils.createScriptEngine(scriptEngineName, scriptSource);
+ }
+ Object evalPortlet = ScriptEngineUtils.evaluateScript(scriptEngine, scriptSource, evalKey, true);
+ scriptSourceLastEvaluated = System.currentTimeMillis();
+
+ if (evalPortlet instanceof Portlet)
+ {
+ tempScriptPortletInstance = (Portlet) evalPortlet;
+ }
+ else if (evalPortlet instanceof Class)
+ {
+ Class<? extends Portlet> scriptPortletClass = (Class<? extends Portlet>) evalPortlet;
+ tempScriptPortletInstance = (Portlet) scriptPortletClass.newInstance();
+ }
+ else
+ {
+ throw new ScriptException("The evaluated return is neither class nor instance of javax.portlet.Portlet. " + evalPortlet);
+ }
+
+ scriptGenericPortletInstance = null;
+ portletDoEditMethod = null;
- try
+ if (tempScriptPortletInstance instanceof GenericPortlet)
{
- Method doEditMethod = scriptGenericPortletInstance.getClass().getMethod("doEdit", new Class [] { RenderRequest.class, RenderResponse.class });
+ scriptGenericPortletInstance = (GenericPortlet) tempScriptPortletInstance;
- if (Modifier.isPublic(doEditMethod.getModifiers()))
+ try
+ {
+ Method doEditMethod = scriptGenericPortletInstance.getClass().getMethod("doEdit", new Class [] { RenderRequest.class, RenderResponse.class });
+
+ if (Modifier.isPublic(doEditMethod.getModifiers()))
+ {
+ portletDoEditMethod = doEditMethod;
+ }
+ }
+ catch (NoSuchMethodException e)
{
- portletDoEditMethod = doEditMethod;
}
}
- catch (NoSuchMethodException e)
- {
- }
+
+ tempScriptPortletInstance.init(portletConfig);
+
+ scriptPortletInstance = tempScriptPortletInstance;
}
-
- scriptSourceLastModified = scriptSource.lastModified();
- tempScriptPortletInstance.init(portletConfig);
-
- scriptPortletInstance = tempScriptPortletInstance;
}
catch (Exception ex)
{
@@ -502,13 +562,25 @@ public class ScriptPortlet extends Gener
return;
}
- boolean createNew = (validatorInstance == null);
+ boolean scriptCreated = (validatorInstance != null);
+ boolean scriptModified = false;
+ long checkedScriptSourceLastModified = 0L;
- if (!createNew)
+ if (scriptCreated)
{
- createNew = (autoRefresh && ScriptEngineUtils.isScriptModified(validatorSource, validatorSourceLastModified));
+ if (autoRefresh)
+ {
+ if ((refreshDelay <= 0L) || (System.currentTimeMillis() - validatorSourceLastEvalStarted > refreshDelay))
+ {
+ scriptModified = (ScriptEngineUtils.isScriptModified(validatorSource, validatorSourceLastModified));
+ }
+ }
- if (!createNew)
+ if (scriptModified)
+ {
+ checkedScriptSourceLastModified = validatorSource.lastModified();
+ }
+ else
{
return;
}
@@ -516,28 +588,35 @@ public class ScriptPortlet extends Gener
try
{
- PreferencesValidator tempValidatorInstance = null;
-
- Object evalPortlet = ScriptEngineUtils.evaluateScript(scriptEngine, validatorSource, evalKey, true);
- validatorSourceLastEvaluated = System.currentTimeMillis();
-
- if (evalPortlet instanceof PreferencesValidator)
+ synchronized (this)
{
- tempValidatorInstance = (PreferencesValidator) evalPortlet;
- }
- else if (evalPortlet instanceof Class)
- {
- Class<? extends PreferencesValidator> validatorClass = (Class<? extends PreferencesValidator>) evalPortlet;
- tempValidatorInstance = (PreferencesValidator) validatorClass.newInstance();
- }
- else
- {
- throw new ScriptException("The evaluated return is neither class nor instance of javax.portlet.PreferencesValidator. " + evalPortlet);
+ if ((scriptModified && validatorSourceLastEvaluated >= checkedScriptSourceLastModified) || (!scriptCreated && validatorInstance != null))
+ {
+ return;
+ }
+
+ validatorSourceLastModified = checkedScriptSourceLastModified;
+ validatorSourceLastEvalStarted = System.currentTimeMillis();
+ PreferencesValidator tempValidatorInstance = null;
+ Object evalPortlet = ScriptEngineUtils.evaluateScript(scriptEngine, validatorSource, evalKey, true);
+ validatorSourceLastEvaluated = System.currentTimeMillis();
+
+ if (evalPortlet instanceof PreferencesValidator)
+ {
+ tempValidatorInstance = (PreferencesValidator) evalPortlet;
+ }
+ else if (evalPortlet instanceof Class)
+ {
+ Class<? extends PreferencesValidator> validatorClass = (Class<? extends PreferencesValidator>) evalPortlet;
+ tempValidatorInstance = (PreferencesValidator) validatorClass.newInstance();
+ }
+ else
+ {
+ throw new ScriptException("The evaluated return is neither class nor instance of javax.portlet.PreferencesValidator. " + evalPortlet);
+ }
+
+ validatorInstance = tempValidatorInstance;
}
-
- validatorSourceLastModified = validatorSource.lastModified();
-
- validatorInstance = tempValidatorInstance;
}
catch (Exception ex)
{
@@ -545,8 +624,53 @@ public class ScriptPortlet extends Gener
}
}
- protected static PreferencesValidator getScriptPreferencesValidator()
+ protected Portlet getScriptPortletInstance()
+ {
+ return scriptPortletInstance;
+ }
+
+ protected long getScriptSourceLastEvaluated()
+ {
+ return scriptSourceLastEvaluated;
+ }
+
+ protected long setRefreshDelay()
+ {
+ return refreshDelay;
+ }
+
+ protected void setRefreshDelay(long refreshDelay)
+ {
+ this.refreshDelay = refreshDelay;
+ }
+
+ protected ScriptSource getScriptSource()
+ {
+ return scriptSource;
+ }
+
+ protected ScriptSource getValidatorSource()
+ {
+ return validatorSource;
+ }
+
+ protected PreferencesValidator getValidatorInstance()
+ {
+ return validatorInstance;
+ }
+
+ protected ScriptSourceFactory getScriptSourceFactory()
+ {
+ return scriptSourceFactory;
+ }
+
+ protected void setScriptSourceFactory(ScriptSourceFactory scriptSourceFactory)
+ {
+ this.scriptSourceFactory = scriptSourceFactory;
+ }
+
+ protected static PreferencesValidator getCurrentValidatorInstance()
{
- return (PreferencesValidator) tlValidatorInstance.get();
+ return (PreferencesValidator) tlCurrentValidatorInstance.get();
}
}
Modified: portals/bridges/bridges-script/trunk/portlet/src/main/java/org/apache/portals/bridges/script/ScriptPortletPreferencesValidator.java
URL: http://svn.apache.org/viewvc/portals/bridges/bridges-script/trunk/portlet/src/main/java/org/apache/portals/bridges/script/ScriptPortletPreferencesValidator.java?rev=937248&r1=937247&r2=937248&view=diff
==============================================================================
--- portals/bridges/bridges-script/trunk/portlet/src/main/java/org/apache/portals/bridges/script/ScriptPortletPreferencesValidator.java (original)
+++ portals/bridges/bridges-script/trunk/portlet/src/main/java/org/apache/portals/bridges/script/ScriptPortletPreferencesValidator.java Fri Apr 23 11:06:13 2010
@@ -30,7 +30,7 @@ public class ScriptPortletPreferencesVal
{
public void validate(PortletPreferences preferences) throws ValidatorException
{
- PreferencesValidator delegatee = ScriptPortlet.getScriptPreferencesValidator();
+ PreferencesValidator delegatee = ScriptPortlet.getCurrentValidatorInstance();
if (delegatee != null)
{
Added: portals/bridges/bridges-script/trunk/portlet/src/main/java/org/apache/portals/bridges/script/ScriptSource.java
URL: http://svn.apache.org/viewvc/portals/bridges/bridges-script/trunk/portlet/src/main/java/org/apache/portals/bridges/script/ScriptSource.java?rev=937248&view=auto
==============================================================================
--- portals/bridges/bridges-script/trunk/portlet/src/main/java/org/apache/portals/bridges/script/ScriptSource.java (added)
+++ portals/bridges/bridges-script/trunk/portlet/src/main/java/org/apache/portals/bridges/script/ScriptSource.java Fri Apr 23 11:06:13 2010
@@ -0,0 +1,79 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.portals.bridges.script;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+/**
+ * ScriptSource
+ * <P>
+ * Representing a script source.
+ * </P>
+ *
+ * @author <a href="mailto:woonsan@apache.org">Woonsan Ko</a>
+ * @version $Id$
+ */
+public interface ScriptSource
+{
+ /**
+ * Returns the name part of the script.
+ * @return
+ */
+ public String getName();
+
+ /**
+ * Returns the extension of the script without leading dot.
+ * @return
+ */
+ public String getExtension();
+
+ /**
+ * Returns the character encoding of the script content.
+ * If the character encoding of the script content is unknown, returns null.
+ * @return
+ */
+ public String getCharacterEncoding();
+
+ /**
+ * Sets the mime type of this script.
+ * @param mimeType
+ */
+ public void setMimeType(String mimeType);
+
+ /**
+ * Returns the mime type of this script.
+ * If the mime type of the script is not set or unknown, returns null.
+ * @return
+ */
+ public String getMimeType();
+
+ /**
+ * Returns the time that the script file was last modified if accessible.
+ * If the script file is not accessible, returns 0L.
+ * @return
+ */
+ public long lastModified();
+
+ /**
+ * Returns an input stream to read this script.
+ * @return
+ * @throws IOException
+ */
+ public InputStream getInputStream() throws IOException;
+
+}
Propchange: portals/bridges/bridges-script/trunk/portlet/src/main/java/org/apache/portals/bridges/script/ScriptSource.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: portals/bridges/bridges-script/trunk/portlet/src/main/java/org/apache/portals/bridges/script/ScriptSource.java
------------------------------------------------------------------------------
svn:keywords = Id
Propchange: portals/bridges/bridges-script/trunk/portlet/src/main/java/org/apache/portals/bridges/script/ScriptSource.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: portals/bridges/bridges-script/trunk/portlet/src/main/java/org/apache/portals/bridges/script/ScriptSourceFactory.java
URL: http://svn.apache.org/viewvc/portals/bridges/bridges-script/trunk/portlet/src/main/java/org/apache/portals/bridges/script/ScriptSourceFactory.java?rev=937248&view=auto
==============================================================================
--- portals/bridges/bridges-script/trunk/portlet/src/main/java/org/apache/portals/bridges/script/ScriptSourceFactory.java (added)
+++ portals/bridges/bridges-script/trunk/portlet/src/main/java/org/apache/portals/bridges/script/ScriptSourceFactory.java Fri Apr 23 11:06:13 2010
@@ -0,0 +1,58 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.portals.bridges.script;
+
+import java.io.IOException;
+
+/**
+ * ScriptSourceFactory
+ * <P>
+ * Representing a script source factory
+ * </P>
+ *
+ * @author <a href="mailto:woonsan@apache.org">Woonsan Ko</a>
+ * @version $Id$
+ */
+public interface ScriptSourceFactory
+{
+ /**
+ * Creates a script source based on uri
+ * @param uri
+ * @return
+ * @throws IOException
+ */
+ public ScriptSource createScriptSource(final String uri) throws IOException;
+
+ /**
+ * Creates a script source based on uri and uriEncoding.
+ * @param uri
+ * @param uriEncoding
+ * @return
+ * @throws IOException
+ */
+ public ScriptSource createScriptSource(final String uri, final String uriEncoding) throws IOException;
+
+ /**
+ * Creates a script source based on uri, uriEncoding and characterEncoding.
+ * @param uri
+ * @param uriEncoding
+ * @param characterEncoding
+ * @return
+ * @throws IOException
+ */
+ public ScriptSource createScriptSource(final String uri, final String uriEncoding, final String characterEncoding) throws IOException;
+}
Propchange: portals/bridges/bridges-script/trunk/portlet/src/main/java/org/apache/portals/bridges/script/ScriptSourceFactory.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: portals/bridges/bridges-script/trunk/portlet/src/main/java/org/apache/portals/bridges/script/ScriptSourceFactory.java
------------------------------------------------------------------------------
svn:keywords = Id
Propchange: portals/bridges/bridges-script/trunk/portlet/src/main/java/org/apache/portals/bridges/script/ScriptSourceFactory.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Copied: portals/bridges/bridges-script/trunk/portlet/src/main/java/org/apache/portals/bridges/script/SimpleScriptSource.java (from r937175, portals/bridges/bridges-script/trunk/portlet/src/main/java/org/apache/portals/bridges/script/ScriptSource.java)
URL: http://svn.apache.org/viewvc/portals/bridges/bridges-script/trunk/portlet/src/main/java/org/apache/portals/bridges/script/SimpleScriptSource.java?p2=portals/bridges/bridges-script/trunk/portlet/src/main/java/org/apache/portals/bridges/script/SimpleScriptSource.java&p1=portals/bridges/bridges-script/trunk/portlet/src/main/java/org/apache/portals/bridges/script/ScriptSource.java&r1=937175&r2=937248&rev=937248&view=diff
==============================================================================
--- portals/bridges/bridges-script/trunk/portlet/src/main/java/org/apache/portals/bridges/script/ScriptSource.java (original)
+++ portals/bridges/bridges-script/trunk/portlet/src/main/java/org/apache/portals/bridges/script/SimpleScriptSource.java Fri Apr 23 11:06:13 2010
@@ -16,31 +16,24 @@
*/
package org.apache.portals.bridges.script;
-import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
-import java.net.URL;
-import java.net.URLDecoder;
/**
- * ScriptSource
+ * SimpleScriptSource
* <P>
- * Representing a script source which could be loaded from a file system, classpath resource.
+ * Simple ScriptSource implementation to load scripts from a file system, classpath resource.
* </P>
*
* @author <a href="mailto:woonsan@apache.org">Woonsan Ko</a>
* @version $Id$
*/
-public class ScriptSource
+public class SimpleScriptSource implements ScriptSource
{
- /** Script source URI. e.g., "file://path", "classpath:path" or "file_relative_path". */
- private String uri;
-
/** The file name part of the script source. */
private String name;
@@ -59,126 +52,13 @@ public class ScriptSource
/** The script content bytes if the script source file is not accessible. */
private byte [] scriptBytes;
- public ScriptSource(String uri) throws IOException
- {
- this(uri, null, null);
- }
-
- public ScriptSource(String uri, String uriEncoding, String characterEncoding) throws IOException
+ public SimpleScriptSource(String name, String extension, String characterEncoding, File scriptFile, byte [] scriptContent) throws IOException
{
- this.uri = uri;
+ this.name = name;
+ this.extension = extension;
this.characterEncoding = characterEncoding;
-
- if (uri.startsWith("file:"))
- {
- String decodedUri = uri;
-
- if (uriEncoding != null)
- {
- decodedUri = URLDecoder.decode(uri, uriEncoding);
- }
-
- scriptFile = new File(decodedUri.substring(5));
- }
- else if (uri.startsWith("classpath:"))
- {
- URL resourceURL = Thread.currentThread().getContextClassLoader().getResource(uri.substring(10));
- String resourceURLString = resourceURL.toString();
-
- if (resourceURLString.startsWith("file:"))
- {
- String decodedUri = resourceURLString;
-
- if (uriEncoding != null)
- {
- decodedUri = URLDecoder.decode(resourceURLString, uriEncoding);
- }
-
- scriptFile = new File(decodedUri.substring(5));
- }
- else
- {
- InputStream is = null;
- BufferedInputStream bis = null;
- ByteArrayOutputStream baos = null;
-
- try
- {
- is = resourceURL.openStream();
- bis = new BufferedInputStream(is);
- baos = new ByteArrayOutputStream(4096);
-
- byte [] bytes = new byte[4096];
-
- int readLen = bis.read(bytes, 0, 4096);
- while (readLen != -1)
- {
- baos.write(bytes, 0, readLen);
- readLen = bis.read(bytes, 0, 4096);
- }
-
- scriptBytes = baos.toByteArray();
- }
- finally
- {
- if (baos != null)
- {
- try
- {
- baos.close();
- }
- catch (Exception ignore) { }
- }
- if (bis != null)
- {
- try
- {
- bis.close();
- }
- catch (Exception ignore) { }
- }
- if (is != null)
- {
- try
- {
- is.close();
- }
- catch (Exception ignore) { }
- }
- }
- }
- }
- else
- {
- String relPath = uri;
- scriptFile = new File(relPath);
- }
-
- if (scriptFile != null)
- {
- name = scriptFile.getName();
- }
- else
- {
- name = uri;
- int offset = name.lastIndexOf('/');
- if (offset != -1)
- {
- name = name.substring(offset + 1);
- }
- }
-
- int offset = name.lastIndexOf('.');
-
- if (offset != -1)
- {
- extension = name.substring(offset + 1);
- }
- }
-
- public String getUri()
- {
- return uri;
+ this.scriptFile = scriptFile;
+ this.scriptBytes = scriptContent;
}
public String getName()
Propchange: portals/bridges/bridges-script/trunk/portlet/src/main/java/org/apache/portals/bridges/script/SimpleScriptSource.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: portals/bridges/bridges-script/trunk/portlet/src/main/java/org/apache/portals/bridges/script/SimpleScriptSource.java
------------------------------------------------------------------------------
svn:keywords = Id
Added: portals/bridges/bridges-script/trunk/portlet/src/main/java/org/apache/portals/bridges/script/SimpleScriptSourceFactory.java
URL: http://svn.apache.org/viewvc/portals/bridges/bridges-script/trunk/portlet/src/main/java/org/apache/portals/bridges/script/SimpleScriptSourceFactory.java?rev=937248&view=auto
==============================================================================
--- portals/bridges/bridges-script/trunk/portlet/src/main/java/org/apache/portals/bridges/script/SimpleScriptSourceFactory.java (added)
+++ portals/bridges/bridges-script/trunk/portlet/src/main/java/org/apache/portals/bridges/script/SimpleScriptSourceFactory.java Fri Apr 23 11:06:13 2010
@@ -0,0 +1,164 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.portals.bridges.script;
+
+import java.io.BufferedInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.net.URLDecoder;
+
+/**
+ * ScriptSourceFactory
+ * <P>
+ * Simple factory class to create ScriptSource instances by using SimpleScriptSource class.
+ * </P>
+ *
+ * @author <a href="mailto:woonsan@apache.org">Woonsan Ko</a>
+ * @version $Id$
+ */
+public class SimpleScriptSourceFactory implements ScriptSourceFactory
+{
+
+ public ScriptSource createScriptSource(final String uri) throws IOException
+ {
+ return createScriptSource(uri, null);
+ }
+
+ public ScriptSource createScriptSource(final String uri, final String uriEncoding) throws IOException
+ {
+ return createScriptSource(uri, uriEncoding, null);
+ }
+
+ public ScriptSource createScriptSource(final String uri, final String uriEncoding, final String characterEncoding) throws IOException
+ {
+ String name = null;
+ String extension = null;
+ File scriptFile = null;
+ byte [] scriptContent = null;
+
+ if (uri.startsWith("file:"))
+ {
+ String decodedUri = uri;
+
+ if (uriEncoding != null)
+ {
+ decodedUri = URLDecoder.decode(uri, uriEncoding);
+ }
+
+ scriptFile = new File(decodedUri.substring(5));
+ }
+ else if (uri.startsWith("classpath:"))
+ {
+ URL resourceURL = Thread.currentThread().getContextClassLoader().getResource(uri.substring(10));
+ String resourceURLString = resourceURL.toString();
+
+ if (resourceURLString.startsWith("file:"))
+ {
+ String decodedUri = resourceURLString;
+
+ if (uriEncoding != null)
+ {
+ decodedUri = URLDecoder.decode(resourceURLString, uriEncoding);
+ }
+
+ scriptFile = new File(decodedUri.substring(5));
+ }
+ else
+ {
+ InputStream is = null;
+ BufferedInputStream bis = null;
+ ByteArrayOutputStream baos = null;
+
+ try
+ {
+ is = resourceURL.openStream();
+ bis = new BufferedInputStream(is);
+ baos = new ByteArrayOutputStream(4096);
+
+ byte [] bytes = new byte[4096];
+
+ int readLen = bis.read(bytes, 0, 4096);
+ while (readLen != -1)
+ {
+ baos.write(bytes, 0, readLen);
+ readLen = bis.read(bytes, 0, 4096);
+ }
+
+ scriptContent = baos.toByteArray();
+ }
+ finally
+ {
+ if (baos != null)
+ {
+ try
+ {
+ baos.close();
+ }
+ catch (Exception ignore) { }
+ }
+ if (bis != null)
+ {
+ try
+ {
+ bis.close();
+ }
+ catch (Exception ignore) { }
+ }
+ if (is != null)
+ {
+ try
+ {
+ is.close();
+ }
+ catch (Exception ignore) { }
+ }
+ }
+ }
+ }
+ else
+ {
+ String relPath = uri;
+ scriptFile = new File(relPath);
+ }
+
+ if (scriptFile != null)
+ {
+ name = scriptFile.getName();
+ }
+ else
+ {
+ name = uri;
+ int offset = name.lastIndexOf('/');
+ if (offset != -1)
+ {
+ name = name.substring(offset + 1);
+ }
+ }
+
+ int offset = name.lastIndexOf('.');
+
+ if (offset != -1)
+ {
+ extension = name.substring(offset + 1);
+ }
+
+ return new SimpleScriptSource(name, extension, characterEncoding, scriptFile, scriptContent);
+ }
+}
Propchange: portals/bridges/bridges-script/trunk/portlet/src/main/java/org/apache/portals/bridges/script/SimpleScriptSourceFactory.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: portals/bridges/bridges-script/trunk/portlet/src/main/java/org/apache/portals/bridges/script/SimpleScriptSourceFactory.java
------------------------------------------------------------------------------
svn:keywords = Id
Propchange: portals/bridges/bridges-script/trunk/portlet/src/main/java/org/apache/portals/bridges/script/SimpleScriptSourceFactory.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: portals/bridges/bridges-script/trunk/portlet/src/test/java/org/apache/portals/bridges/script/AbstractSimpleScriptPortletTestCase.java
URL: http://svn.apache.org/viewvc/portals/bridges/bridges-script/trunk/portlet/src/test/java/org/apache/portals/bridges/script/AbstractSimpleScriptPortletTestCase.java?rev=937248&r1=937247&r2=937248&view=diff
==============================================================================
--- portals/bridges/bridges-script/trunk/portlet/src/test/java/org/apache/portals/bridges/script/AbstractSimpleScriptPortletTestCase.java (original)
+++ portals/bridges/bridges-script/trunk/portlet/src/test/java/org/apache/portals/bridges/script/AbstractSimpleScriptPortletTestCase.java Fri Apr 23 11:06:13 2010
@@ -20,6 +20,7 @@ import static org.easymock.EasyMock.crea
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.replay;
+import java.io.IOException;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
@@ -84,10 +85,11 @@ public abstract class AbstractSimpleScri
try
{
+ scriptPortlet.setScriptSourceFactory(new LastModifiedMutableScriptSourceFactory());
scriptPortlet.init(portletConfig);
- scriptPortletInstance = scriptPortlet.scriptPortletInstance;
- scriptSourceLastEvaluated = scriptPortlet.scriptSourceLastEvaluated;
- assertTrue(scriptPortlet.scriptSourceLastEvaluated > 0L);
+ scriptPortletInstance = scriptPortlet.getScriptPortletInstance();
+ scriptSourceLastEvaluated = scriptPortlet.getScriptSourceLastEvaluated();
+ assertTrue(scriptPortlet.getScriptSourceLastEvaluated() > 0L);
}
catch (PortletException e)
{
@@ -129,7 +131,7 @@ public abstract class AbstractSimpleScri
try
{
helloScriptPortlet.init(portletConfig);
- assertTrue(helloScriptPortlet.scriptSourceLastEvaluated > 0L);
+ assertTrue(helloScriptPortlet.getScriptSourceLastEvaluated() > 0L);
}
catch (PortletException e)
{
@@ -154,7 +156,7 @@ public abstract class AbstractSimpleScri
try
{
helloScriptPortlet.init(portletConfig);
- assertTrue(helloScriptPortlet.scriptSourceLastEvaluated > 0L);
+ assertTrue(helloScriptPortlet.getScriptSourceLastEvaluated() > 0L);
}
catch (PortletException e)
{
@@ -186,7 +188,7 @@ public abstract class AbstractSimpleScri
try
{
helloScriptPortlet.init(portletConfig);
- assertTrue(helloScriptPortlet.scriptSourceLastEvaluated > 0L);
+ assertTrue(helloScriptPortlet.getScriptSourceLastEvaluated() > 0L);
}
catch (PortletException e)
{
@@ -206,27 +208,49 @@ public abstract class AbstractSimpleScri
scriptPortlet.render(request, response);
assertFalse("The script is refreshed " +
- "(scriptSourceLastEvaluated: " + scriptPortlet.scriptSourceLastEvaluated + ", " +
+ "(scriptSourceLastEvaluated: " + scriptPortlet.getScriptSourceLastEvaluated() + ", " +
"initial-scriptSourceLastEvaluated: " + scriptSourceLastEvaluated + ")",
- scriptPortlet.scriptSourceLastEvaluated > scriptSourceLastEvaluated);
+ scriptPortlet.getScriptSourceLastEvaluated() > scriptSourceLastEvaluated);
String content = response.getContentAsString();
assertNotNull(content);
//System.out.println("### content: " + content);
String greeting = "<H1>" + portletConfig.getResourceBundle(Locale.ENGLISH).getString("javax.portlet.title") + "</H1>";
assertEquals(greeting, content.trim());
-
- // to force to refresh...
+
+ // to check not refreshed...
Thread.sleep(10);
- scriptPortlet.scriptSourceLastModified = 0L;
scriptPortlet.render(request, response);
+ assertSame(scriptPortletInstance, scriptPortlet.getScriptPortletInstance());
+ assertTrue("The script is refreshed " +
+ "(scriptSourceLastEvaluated: " + scriptPortlet.getScriptSourceLastEvaluated() + ", " +
+ "initial-scriptSourceLastEvaluated: " + scriptSourceLastEvaluated + ")",
+ scriptPortlet.getScriptSourceLastEvaluated() == scriptSourceLastEvaluated);
- assertNotSame(scriptPortletInstance, scriptPortlet.scriptPortletInstance);
-
+ // to force to refresh...
+ Thread.sleep(10);
+ scriptPortlet.setRefreshDelay(0L);
+ ((LastModifiedMutableScriptSource) scriptPortlet.getScriptSource()).setLastModified(System.currentTimeMillis());
+ scriptPortlet.render(request, response);
+ assertNotSame(scriptPortletInstance, scriptPortlet.getScriptPortletInstance());
assertTrue("The script is not refreshed " +
- "(scriptSourceLastEvaluated: " + scriptPortlet.scriptSourceLastEvaluated + ", " +
+ "(scriptSourceLastEvaluated: " + scriptPortlet.getScriptSourceLastEvaluated() + ", " +
"initial-scriptSourceLastEvaluated: " + scriptSourceLastEvaluated + ")",
- scriptPortlet.scriptSourceLastEvaluated > scriptSourceLastEvaluated);
+ scriptPortlet.getScriptSourceLastEvaluated() > scriptSourceLastEvaluated);
+
+ // to check not refreshed when refresh delay is a big number...
+ Thread.sleep(10);
+ scriptPortletInstance = scriptPortlet.getScriptPortletInstance();
+ scriptSourceLastEvaluated = scriptPortlet.getScriptSourceLastEvaluated();
+ scriptPortlet.setRefreshDelay(600000L);
+ ((LastModifiedMutableScriptSource) scriptPortlet.getScriptSource()).setLastModified(System.currentTimeMillis());
+ scriptPortlet.render(request, response);
+ assertSame(scriptPortletInstance, scriptPortlet.getScriptPortletInstance());
+ assertTrue("The script is refreshed " +
+ "(scriptSourceLastEvaluated: " + scriptPortlet.getScriptSourceLastEvaluated() + ", " +
+ "initial-scriptSourceLastEvaluated: " + scriptSourceLastEvaluated + ")",
+ scriptPortlet.getScriptSourceLastEvaluated() == scriptSourceLastEvaluated);
+
}
public void testProcessAction() throws Exception
@@ -234,9 +258,9 @@ public abstract class AbstractSimpleScri
MockActionRequest request = new MockActionRequest(portletContext);
MockPortletPreferences preferences = new MockPortletPreferences();
- if (scriptPortlet.validatorInstance != null)
+ if (scriptPortlet.getValidatorInstance() != null)
{
- preferences.setPreferencesValidator(scriptPortlet.validatorInstance);
+ preferences.setPreferencesValidator(scriptPortlet.getValidatorInstance());
}
request.setPreferences(preferences);
@@ -298,4 +322,42 @@ public abstract class AbstractSimpleScri
scriptPortlet.serveResource(request, response);
assertEquals("hello", values.get("message"));
}
+
+ private static class LastModifiedMutableScriptSource extends DelegatingScriptSourceAdaptor
+ {
+ private long lastModified = -1L;
+
+ public LastModifiedMutableScriptSource(final ScriptSource delegatee) throws IOException
+ {
+ super(delegatee);
+ }
+
+ @Override
+ public long lastModified()
+ {
+ if (-1L == lastModified)
+ {
+ return super.lastModified();
+ }
+ else
+ {
+ return lastModified;
+ }
+ }
+
+ private void setLastModified(long lastModified)
+ {
+ this.lastModified = lastModified;
+ }
+ }
+
+ private static class LastModifiedMutableScriptSourceFactory extends SimpleScriptSourceFactory
+ {
+ @Override
+ public ScriptSource createScriptSource(final String uri, final String uriEncoding, final String characterEncoding) throws IOException
+ {
+ ScriptSource scriptSource = super.createScriptSource(uri, uriEncoding, characterEncoding);
+ return new LastModifiedMutableScriptSource(scriptSource);
+ }
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: bridges-commits-unsubscribe@portals.apache.org
For additional commands, e-mail: bridges-commits-help@portals.apache.org