Revision: 1331
Author:   ge0ffrey
Date:     2006-08-22 02:44:35 -0700 (Tue, 22 Aug 2006)
ViewCVS:  http://svn.sourceforge.net/spring-rich-c/?rev=1331&view=rev

Log Message:
-----------
JnlpPropertyPlaceholderConfigurer

Modified Paths:
--------------
    trunk/spring-richclient/pom.xml
    trunk/spring-richclient/tiger/pom.xml

Added Paths:
-----------
    
trunk/spring-richclient/tiger/src/main/java/org/springframework/richclient/jnlp/
    
trunk/spring-richclient/tiger/src/main/java/org/springframework/richclient/jnlp/JnlpPropertyPlaceholderConfigurer.java
Modified: trunk/spring-richclient/pom.xml
===================================================================
--- trunk/spring-richclient/pom.xml     2006-08-22 07:15:22 UTC (rev 1330)
+++ trunk/spring-richclient/pom.xml     2006-08-22 09:44:35 UTC (rev 1331)
@@ -873,14 +873,14 @@
             </dependency>
 
             <!-- JNLP -->
-            <!--<dependency>-->
-                <!--<groupId>javax.jnlp</groupId>-->
-                <!--<artifactId>jnlp</artifactId>-->
-                <!--<version>1.5.0</version>-->
-                <!--<scope>system</scope>-->
-                <!--<systemPath>${java.home}/lib/javaws.jar</systemPath>-->
+            <dependency>
+                <groupId>javax.jnlp</groupId>
+                <artifactId>jnlp</artifactId>
+                <version>1.5.0</version>
+                <scope>system</scope>
+                <systemPath>${java.home}/lib/javaws.jar</systemPath>
                 <!-- This isn't purely jnlp, it contains sun files... -->
-            <!--</dependency>-->
+            </dependency>
 
             <!-- Persistence (for samples) -->
             <dependency>

Modified: trunk/spring-richclient/tiger/pom.xml
===================================================================
--- trunk/spring-richclient/tiger/pom.xml       2006-08-22 07:15:22 UTC (rev 
1330)
+++ trunk/spring-richclient/tiger/pom.xml       2006-08-22 09:44:35 UTC (rev 
1331)
@@ -41,7 +41,8 @@
     </reporting>
 
     <dependencies>
-    
+
+        <!-- Internal -->
         <dependency>
             <groupId>org.springframework.richclient</groupId>
             <artifactId>spring-richclient-support</artifactId>
@@ -53,10 +54,17 @@
             <scope>test</scope>
         </dependency>
 
+        <!-- Dependency injection -->
         <dependency>
             <groupId>org.springframework</groupId>
             <artifactId>spring-context</artifactId>
         </dependency>
+
+        <!-- JNLP -->
+        <dependency>
+            <groupId>javax.jnlp</groupId>
+            <artifactId>jnlp</artifactId>
+        </dependency>
         
     </dependencies>
 

Added: 
trunk/spring-richclient/tiger/src/main/java/org/springframework/richclient/jnlp/JnlpPropertyPlaceholderConfigurer.java
===================================================================
--- 
trunk/spring-richclient/tiger/src/main/java/org/springframework/richclient/jnlp/JnlpPropertyPlaceholderConfigurer.java
                              (rev 0)
+++ 
trunk/spring-richclient/tiger/src/main/java/org/springframework/richclient/jnlp/JnlpPropertyPlaceholderConfigurer.java
      2006-08-22 09:44:35 UTC (rev 1331)
@@ -0,0 +1,150 @@
+package org.springframework.richclient.jnlp;
+
+import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
+
+import javax.jnlp.BasicService;
+import javax.jnlp.ServiceManager;
+import javax.jnlp.UnavailableServiceException;
+import java.util.Properties;
+
+/**
+ * Subclass of PropertyPlaceholderConfigurer that resolves the following 
properties as placeholders:
+ * <ul>
+ * <li><code>jnlp.webAppContextUrl</code> the webAppContextUrl, for example
+ * http://domain.com/petclinic/</li>
+ * </ul>
+ * <p/>
+ * <p>Can be combined with "locations" and/or "properties" values.
+ * <p/>
+ * <p>If a placeholder could not be resolved against the provided local
+ * properties within the application, this configurer will fall back to
+ * the JNLP properties. Can also be configured to let jnlp properties
+ * override local properties (contextOverride=true).
+ * <p/>
+ * <p>If not running within a JNLP context (or any other context that
+ * is able to satisfy the BasicService.lookup call), this class will
+ * use the fallBackWebAppContextUrl. This allows for keeping
+ * JnlpPropertyPlaceholderConfigurer definitions in test suites.
+ * <p/>
+ * <p> A typical usage would be:
+ * <pre>
+ * &lt;bean id="jnlpPropertyPlaceholderConfigurer"
+ *      
class="be.kahosl.thot.swingui.util.JnlpPropertyPlaceholderConfigurer"&gt;
+ *      &lt;property name="fallBackWebAppContextUrl" 
value="http://localhost:8080/mywebapp/"/&gt;
+ *      &lt;property name="jnlpRelativeDirectoryPathFromWebAppContext" 
value="/jnlp/"/&gt;
+ *  &lt;/bean&gt;
+ *  </pre>
+ * <p/>
+ * Use this in combination with Sun's JnlpDownloadServlet to not have to 
hardcode your server URL:
+ * 
http://java.sun.com/j2se/1.5.0/docs/guide/javaws/developersguide/downloadservletguide.html
+ *
+ * @author Geoffrey De Smet
+ * @see #setLocations
+ * @see #setProperties
+ * @see #setSystemPropertiesModeName
+ * @see #setContextOverride
+ * @see javax.jnlp.BasicService#getCodeBase()
+ */
+public class JnlpPropertyPlaceholderConfigurer extends 
PropertyPlaceholderConfigurer {
+
+    /**
+     * The placeholder for getWebAppContextUrl()
+     */
+    public static final String WEB_APP_CONTEXT_URL_PLACEHOLDER = 
"jnlp.webAppContextUrl";
+
+    private boolean contextOverride = false;
+    private String fallBackWebAppContextUrl = "http://localhost:8080/";;
+    private String jnlpRelativeDirectoryPathFromWebAppContext = "/";
+
+    /**
+     * Set whether the JNLP properties should override local properties within 
the application.
+     * Default is "false": JNLP properties settings serve as fallback.
+     * <p>Note that system properties will still override JNLP properties,
+     * if the system properties mode is set to 
"SYSTEM_PROPERTIES_MODE_OVERRIDE".
+     *
+     * @see #setSystemPropertiesModeName
+     * @see #SYSTEM_PROPERTIES_MODE_OVERRIDE
+     */
+    public void setContextOverride(boolean contextOverride) {
+        this.contextOverride = contextOverride;
+    }
+
+    /**
+     * Set the webAppContextUrl to use when no javax.jnlp.BasicService is 
available.
+     * This is usefull for testing outside JNLP (Java webstart).
+     * This defaults to <code>http://localhost:8080/</code>.
+     * Ussually you 'll want to set this to 
<code>http://localhost:8080/mywebapp/</code>.
+     *
+     * @param fallBackWebAppContextUrl the webAppContextUrl to fall back on 
ending with a slash
+     */
+    public void setFallBackWebAppContextUrl(String fallBackWebAppContextUrl) {
+        this.fallBackWebAppContextUrl = fallBackWebAppContextUrl;
+    }
+
+    /**
+     * Sets the relative directory path of the JNLP file relative to the 
WebAppContext.
+     * Default this is <code>/</code>, which means that the JNLP file is in 
the root of your webapp.
+     * If your JNLP file isn't in the root of you webapp, change it.
+     * For example for 
<code>http://localhost:8080/mywebapp/dist/jnlp/myswingapp.jnlp</code>
+     * you would set this to <code>/dist/jnlp/</code>.
+     *
+     * @param jnlpRelativeDirectoryPathFromWebAppContext
+     *         the relative directory path starting and ending with a slash
+     */
+    public void setJnlpRelativeDirectoryPathFromWebAppContext(String 
jnlpRelativeDirectoryPathFromWebAppContext) {
+        this.jnlpRelativeDirectoryPathFromWebAppContext = 
jnlpRelativeDirectoryPathFromWebAppContext;
+    }
+
+    protected String resolvePlaceholder(String placeholder, Properties props) {
+        String value = null;
+        if (this.contextOverride) {
+            value = resolvePlaceholder(placeholder);
+        }
+        if (value == null) {
+            value = super.resolvePlaceholder(placeholder, props);
+        }
+        if (value == null) {
+            value = resolvePlaceholder(placeholder);
+        }
+        return value;
+    }
+
+    /**
+     * Resolves the given placeholder using the jnlp properties.
+     *
+     * @param placeholder the placeholder to resolve
+     * @return the resolved value, of null if none
+     */
+    protected String resolvePlaceholder(String placeholder) {
+        String value = null;
+        if (placeholder.equals(WEB_APP_CONTEXT_URL_PLACEHOLDER)) {
+            value = getWebAppContextUrl();
+        }
+        return value;
+    }
+
+    /**
+     * Uses the jnlp API to determine the webapp context.
+     * If used outside of webstart, <code>fallBackWebAppContextUrl</code> is 
returned.
+     * For example this could return 
<code>http://localhost:8080/mywebapp/</code>.
+     *
+     * @return the url to the webapp ending with a slash
+     */
+    public String getWebAppContextUrl() {
+        String webAppContextUrl;
+        try {
+            BasicService basicService = (BasicService) 
ServiceManager.lookup("javax.jnlp.BasicService");
+            String codeBase = basicService.getCodeBase().toExternalForm();
+            if (!codeBase.endsWith("/")) {
+                codeBase += "/";
+            }
+            int webAppContextUrlLength = 
codeBase.lastIndexOf(jnlpRelativeDirectoryPathFromWebAppContext);
+            webAppContextUrl = codeBase.substring(0, webAppContextUrlLength + 
1);
+        } catch (UnavailableServiceException e) {
+            // TODO logging
+            webAppContextUrl = fallBackWebAppContextUrl;
+        }
+        return webAppContextUrl;
+    }
+
+}


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.


-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
spring-rich-c-cvs mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/spring-rich-c-cvs

Reply via email to