Revision: 3508
Author: lucboudreau
Date: Thu May  6 13:40:41 2010
Log: License checkup classes.
http://code.google.com/p/power-architect/source/detail?r=3508

Added:
/trunk/src/main/java/ca/sqlpower/architect/enterprise/ServerInfoProvider.java
Modified:
 /trunk/build.xml
/trunk/src/main/java/ca/sqlpower/architect/enterprise/ArchitectClientSideSession.java

=======================================
--- /dev/null
+++ /trunk/src/main/java/ca/sqlpower/architect/enterprise/ServerInfoProvider.java Thu May 6 13:40:41 2010
@@ -0,0 +1,217 @@
+/*
+ * Copyright (c) 2009, SQL Power Group Inc.
+ *
+ * This file is part of Wabit.
+ *
+ * Wabit is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Wabit 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package ca.sqlpower.architect.enterprise;
+
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.swing.JOptionPane;
+import javax.swing.SwingUtilities;
+
+import org.apache.http.auth.AuthScope;
+import org.apache.http.auth.UsernamePasswordCredentials;
+import org.apache.http.client.methods.HttpOptions;
+import org.apache.http.client.methods.HttpUriRequest;
+import org.apache.http.impl.client.BasicResponseHandler;
+import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.http.params.BasicHttpParams;
+import org.apache.http.params.HttpConnectionParams;
+import org.apache.http.params.HttpParams;
+import org.json.JSONException;
+import org.json.JSONObject;
+
+import ca.sqlpower.enterprise.client.SPServerInfo;
+import ca.sqlpower.enterprise.client.ServerProperties;
+import ca.sqlpower.util.Version;
+
+public abstract class ServerInfoProvider {
+
+ public static final String defaultWatermarkMessage = "This version of SQL Power Architect is for EVALUATION PURPOSES ONLY. To obtain a full Production License, please visit www.sqlpower.ca/wabit-ep";
+
+ private static Map<String,Version> version = new HashMap<String, Version>();
+
+ private static Map<String,Boolean> licenses = new HashMap<String, Boolean>();
+
+ private static Map<String,String> watermarkMessages = new HashMap<String, String>();
+
+       public static Version getServerVersion(
+                       String host,
+                       String port,
+                       String path,
+                       String username,
+                       String password) throws 
MalformedURLException,IOException
+       {
+               init(toURL(host, port, path), username, password);
+ return version.get(generateServerKey(host, port, path, username, password));
+       }
+
+       public static boolean isServerLicensed(SPServerInfo infos)
+                       throws MalformedURLException,IOException
+       {
+               return isServerLicensed(
+                               infos.getServerAddress(),
+                               String.valueOf(infos.getPort()),
+                               infos.getPath(),
+                               infos.getUsername(),
+                               infos.getPassword());
+       }
+
+       public static boolean isServerLicensed(
+                       String host,
+                       String port,
+                       String path,
+                       String username,
+                       String password) throws 
MalformedURLException,IOException
+       {
+               init(toURL(host, port, path), username, password);
+ return licenses.get(generateServerKey(host, port, path, username, password));
+       }
+
+       private static URL toURL(
+                       String host,
+                       String port,
+                       String path) throws MalformedURLException
+       {
+               // Build the base URL
+               StringBuilder sb = new StringBuilder();
+               sb.append("http://";);
+               sb.append(host);
+               sb.append(":");
+               sb.append(port);
+               sb.append(path);
+               sb.append(path.endsWith("/")?"serverinfo":"/serverinfo");
+
+               // Spawn a connection object
+               return new URL(sb.toString());
+       }
+
+ private static void init(URL url, String username, String password) throws IOException {
+
+ if (version.containsKey(generateServerKey(url, username, password))) return;
+
+               try {
+                       HttpParams params = new BasicHttpParams();
+               HttpConnectionParams.setConnectionTimeout(params, 2000);
+               DefaultHttpClient httpClient = new DefaultHttpClient(params);
+ httpClient.setCookieStore(ArchitectClientSideSession.getCookieStore());
+               httpClient.getCredentialsProvider().setCredentials(
+                   new AuthScope(url.getHost(), AuthScope.ANY_PORT),
+                   new UsernamePasswordCredentials(username, password));
+
+               HttpUriRequest request = new HttpOptions(url.toURI());
+ String responseBody = httpClient.execute(request, new BasicResponseHandler());
+
+                       // Decode the message
+                       String serverVersion;
+                       Boolean licensedServer;
+                       final String watermarkMessage;
+                       try {
+                               JSONObject jsonObject = new 
JSONObject(responseBody);
+ serverVersion = jsonObject.getString(ServerProperties.SERVER_VERSION.toString()); + licensedServer = jsonObject.getBoolean(ServerProperties.SERVER_LICENSED.toString()); + watermarkMessage = jsonObject.getString(ServerProperties.SERVER_WATERMARK_MESSAGE.toString());
+                       } catch (JSONException e) {
+                               throw new IOException(e.getMessage());
+                       }
+
+                       // Save found values
+ version.put(generateServerKey(url, username, password), new Version(serverVersion)); + licenses.put(generateServerKey(url, username, password), licensedServer); + watermarkMessages.put(generateServerKey(url, username, password), watermarkMessage);
+
+                       // Notify the user if the server is not licensed.
+                       if (!licensedServer) {
+                               SwingUtilities.invokeLater(new Runnable() {
+                                       public void run() {
+                                               JOptionPane.showMessageDialog(
+                                                               null,
+                                                               
watermarkMessage,
+                                                               "SQL Power Wabit 
Server License",
+                                                               
JOptionPane.WARNING_MESSAGE);
+                                       }
+                               });
+                       }
+
+               } catch (URISyntaxException e) {
+                       throw new IOException(e.getLocalizedMessage());
+               }
+       }
+
+       public static String getWatermarkMessage(SPServerInfo infos)
+                       throws MalformedURLException,IOException
+       {
+               return getWatermarkMessage(
+                               infos.getServerAddress(),
+                               String.valueOf(infos.getPort()),
+                               infos.getPath(),
+                               infos.getUsername(),
+                               infos.getPassword());
+       }
+
+       public static String getWatermarkMessage(
+                       String host,
+                       String port,
+                       String path,
+                       String username,
+                       String password)
+       {
+               String message = defaultWatermarkMessage;
+               try {
+                       if 
(!isServerLicensed(host,port,path,username,password)) {
+ message = watermarkMessages.get(generateServerKey(host, port, path, username, password));
+                       } else {
+                               message = "";
+                       }
+               } catch (Exception e) {
+                       // no op
+               }
+               return message;
+       }
+
+       private static String generateServerKey(
+                       String host,
+                       String port,
+                       String path,
+                       String username,
+                       String password) throws MalformedURLException
+       {
+               return generateServerKey(
+                       toURL(host, port, path),
+                       username,
+                       password);
+       }
+
+       private static String generateServerKey(
+                       URL url,
+                       String username,
+                       String password)
+       {
+               return
+                       String.valueOf(
+                               url.toString()
+                                       .concat(username)
+                                       .concat(password)
+                               .hashCode());
+       }
+}
=======================================
--- /trunk/build.xml    Mon Apr 26 08:20:24 2010
+++ /trunk/build.xml    Thu May  6 13:40:41 2010
@@ -224,7 +224,7 @@
                </not>
        </condition>

-       <target name="init" depends="checkAntVersion, checkBuildPath"
+       <target name="init" depends="checkAntVersion"
description="Checks build prereqs, creates output dir, and determines product version"
                >
                <tstamp/>
@@ -508,7 +508,7 @@
        <!-- This target is used to make an architect library jar to embed
        in other applications -->
        <target name="library.jar" depends="compile">
-               <jar jarfile="dist/architect_library.jar" basedir="${build}"
+               <jar jarfile="dist/architect-core-${app.version}.jar" 
basedir="${build}"
                        includes="ca/sqlpower/architect/*.class,
                                                
ca/sqlpower/architect/ddl/*.class,
                                                
ca/sqlpower/architect/diff/*.class,
@@ -525,6 +525,9 @@
                        includes="ca/sqlpower/architect/swingui/dbtree/**,
                                  icons/parts/**">
                </jar>
+               <copyfile
+                               
dest="${dist.base}/architect-dbtree-${app.version}.jar"
+                               src="${build}/architect_dbtree_lib.jar"/>
        </target>

        <!-- DOCUMENTATION
@@ -620,7 +623,7 @@
         ** path (e.g., an Eclipse Workspace under "Documents and Settings"
         ** will fail out because it changes spaces in filenames to %20.
         -->
- <target name="help" depends="checkBuildPath, xslt-stripwidths" description="Create JavaHelp"> + <target name="help" depends="xslt-stripwidths" description="Create JavaHelp">
                <mkdir dir="${build}/help"/>
                <!-- This XSLT element is what's causing the problem with the
                        directories with spaces not working. It outputs the 
results
@@ -684,7 +687,7 @@
        </target>

        <!-- Build the HTML -->
- <target name="html" depends="checkBuildPath, xslt-stripwidths" description="Create user guide in HTML"> + <target name="html" depends="xslt-stripwidths" description="Create user guide in HTML">
                <mkdir dir="${build}/ca/sqlpower/architect/doc/" />

                <xslt
@@ -754,7 +757,7 @@

        </target>

-       <target name ="jar" depends="compile">
+ <target name ="jar" depends="compile" description="Builds the embedable JAR">
                <jar jarfile="dist/architect.jar" basedir="${build}"/>
        </target>

@@ -1148,7 +1151,7 @@
                </launch4j>
        </target>

-       <target name="osx_adapter_jar" depends="checkBuildPath"
+       <target name="osx_adapter_jar"
description="Creates the osx_packaging_utils/osx_adapter.jar file (only works on OSX)">
                <javac srcdir="osx_packaging_utils"
                           destdir="${build}"
@@ -1438,62 +1441,6 @@
                </fail>
        </target>

-       <!-- This is needed to prevent a problem with DocBook help file 
generation
-                in which the XSLT replaces the spaces in the path name with 
'%20', and
-                doesn't convert it back when saving the files. It looks like 
this is
-                related to the DocBook chunking functionality.
-                We don't know of any good way to fix this yet, so we have this
-                check for the time being.-->
-       <target name="checkBuildPath"
-               description="Ensure the build path contains no spaces.">
- <echo message="Note: Your build path cannot contain any spaces at this point because of a problem with help file generation."/>
-               <echo message="Checking build path..."/>
-               <!-- Check if the build path is absolute. If so, set buildPath 
-->
-               <condition property="buildPath" value="${build}">
-                       <or>
-                               <!-- Check for Windows style absolute 
pathnames-->
-                               <and>
-                                       <or>
-                                               <!-- pathname with backslash only 
-->
-                                               <matches string="${build}" 
pattern="^\\{1}.*"/>
-                                               <!-- pathname with drive letter 
-->
-                                               <matches string="${build}" 
pattern="^[a-zA-Z]{1}:\\.*"/>
-                                       </or>
-                                       <os family="windows"/>
-                               </and>
-                               <!-- Check for Unix style absolute pathnames -->
-                               <and>
-                                       <matches string="${build}" 
pattern="^//{1}.*"/>
-                                       <not>
-                                               <os family="windows"/>
-                                       </not>
-                               </and>
-                       </or>
-               </condition>
-               <!-- Check if buildPath is set. If not, then it's a relative 
buildpath-->
-               <condition property="buildPath" value="${basedir}\${build}">
-                       <and>
-                               <not>
-                                       <isset property="buildPath"/>
-                               </not>
-                               <os family="windows"/>
-                       </and>
-               </condition>
- <!-- Check if buildPath is set. If not, then it's not a windows buildpath (i.e. doesn't use '\')-->
-               <condition property="buildPath" value="${basedir}/${build}">
-                       <not>
-                               <isset property="buildPath"/>
-                       </not>
-               </condition>
-               <echo message="Your build path is ${buildPath}"/>
-               <!-- Now check the build path for spaces -->
- <fail message="Your build path MUST NOT contain any spaces. See the file 'build.properties.example' to see how to set property 'build' to an absolute pathname that does not contain any spaces">
-               <condition>
-                               <contains string="${buildPath}" substring=" "/>
-               </condition>
-               </fail>
-       </target>
-
        <!--
                This build target checks for the sqlpower-library project, 
which is
                required by the Power*Architect to build.
=======================================
--- /trunk/src/main/java/ca/sqlpower/architect/enterprise/ArchitectClientSideSession.java Thu May 6 09:12:07 2010 +++ /trunk/src/main/java/ca/sqlpower/architect/enterprise/ArchitectClientSideSession.java Thu May 6 13:40:41 2010
@@ -201,6 +201,17 @@
                updater.addListener(revalidateUIComponentsListener);

                jsonPersister = new SPJSONPersister(updater);
+
+               try {
+            ServerInfoProvider.getServerVersion(
+                    projectLocation.getServiceInfo().getServerAddress(),
+ String.valueOf(projectLocation.getServiceInfo().getPort()),
+                    projectLocation.getServiceInfo().getPath(),
+                    projectLocation.getServiceInfo().getUsername(),
+                    projectLocation.getServiceInfo().getPassword());
+        } catch (Exception e) {
+ throw new AssertionError("Exception encountered while verifying the server license:" + e.getMessage());
+        }
        }

        // -

Reply via email to