Author: saminda
Date: Tue Jan 22 04:15:49 2008
New Revision: 12676

Log:

Addded preliminary announcement support per discussion 


Added:
   trunk/registry/modules/core/src/main/java/org/wso2/registry/announcement/
   
trunk/registry/modules/core/src/main/java/org/wso2/registry/announcement/Announcement.java
   
trunk/registry/modules/core/src/main/java/org/wso2/registry/announcement/URLContent.java

Added: 
trunk/registry/modules/core/src/main/java/org/wso2/registry/announcement/Announcement.java
==============================================================================
--- (empty file)
+++ 
trunk/registry/modules/core/src/main/java/org/wso2/registry/announcement/Announcement.java
  Tue Jan 22 04:15:49 2008
@@ -0,0 +1,124 @@
+/*
+ * Copyright 2004,2005 The Apache Software Foundation.
+ *
+ * Licensed 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.wso2.registry.announcement;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.ByteArrayOutputStream;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.net.URLConnection;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * Announcements of WSO2 family of projects will be sent out to the specific 
requestor.
+ * This would only rely on JDK 1.5 and its DOM imples
+ */
+public class Announcement {
+
+    private static Map<String, URLContent> announcementCache =
+            new ConcurrentHashMap<String, URLContent>();
+    /**
+     * TTL - Time to live. Default is 24 hours.
+     */
+    private static long TTL = 1000 * 60 * 60 * 24;
+
+    /**
+     * This method will return a string based on the "announcement" URL. 
Following is the state
+     * space associated with this method.
+     *
+     * Internally content will be obtain by HTTP GET interface. If the process 
is executed
+     * successfully the return string will be cached in announcementCache with 
the key
+     * "announcement". For all successive similar announcement urls, if 
content is available in
+     * announcementCache, it will be send back to the users if and only if the 
time to live (TTL)
+     * does not exceed. Default TTL is 24 hours. For all other error or 
exception that could
+     * result with in the system will cause to send back NULL object for the 
given "announcement".
+     * In this implementation, error will not be recorded.
+     *
+     * @param announcement URL to be located
+     * @return String {Serialized XML}
+     */
+    public String get(String announcement) {
+        if (announcementCache.containsKey(announcement)) {
+            URLContent urlContent = announcementCache.get(announcement);
+            long lastModifiedTime = urlContent.getLastModifiedTime();
+            long currentTime = System.currentTimeMillis();
+            if (currentTime - lastModifiedTime > TTL) {
+                //new one
+                return createContent(announcement);
+            } else {
+                // send the old one;
+                return urlContent.getCachedContent();
+            }
+        } else {
+            // brand new one;
+            return createContent(announcement);
+        }
+    }
+
+    private String createContent(String announcement) {
+        long currentTime = System.currentTimeMillis();
+        String urlContentString;
+        try {
+            urlContentString = getUrlContent(announcement);
+            if (urlContentString == null) {
+                return null;
+            }
+            URLContent urlContent = new URLContent(urlContentString, 
currentTime);
+            announcementCache.put(announcement, urlContent);
+            return urlContentString;
+        } catch (IOException e) {
+            return null;
+        }
+    }
+
+    private String getUrlContent(String url) throws IOException {
+        URL announcementURL = new URL(url);
+        HttpURLConnection httpCon;
+        URLConnection urlConnection = announcementURL.openConnection();
+        if (urlConnection instanceof HttpURLConnection) {
+            httpCon = (HttpURLConnection) urlConnection;
+        } else {
+            return null;
+        }
+        httpCon.setDoOutput(true);
+        httpCon.setDoInput(true);
+        httpCon.setUseCaches(false);
+        httpCon.setRequestMethod("GET");
+        HttpURLConnection.setFollowRedirects(true);
+        httpCon.connect();
+        InputStream in = httpCon.getInputStream();
+        int code = httpCon.getResponseCode();
+        String doc = null;
+        if (code == HttpURLConnection.HTTP_OK) {
+            doc = readContent(in);
+        }
+        httpCon.disconnect();
+        return doc;
+    }
+
+    private String readContent(InputStream in) throws IOException {
+        ByteArrayOutputStream bos = new ByteArrayOutputStream();
+        byte[] buffer = new byte[1024];
+        int c;
+        while ((c = in.read(buffer)) != -1) {
+           bos.write(buffer,0,c);
+        }
+        return new String(bos.toByteArray());
+    }
+}
+

Added: 
trunk/registry/modules/core/src/main/java/org/wso2/registry/announcement/URLContent.java
==============================================================================
--- (empty file)
+++ 
trunk/registry/modules/core/src/main/java/org/wso2/registry/announcement/URLContent.java
    Tue Jan 22 04:15:49 2008
@@ -0,0 +1,38 @@
+/*
+ * Copyright 2004,2005 The Apache Software Foundation.
+ *
+ * Licensed 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.wso2.registry.announcement;
+
+/**
+ *
+ */
+public class URLContent {
+    private String cachedContent;
+    private long lastModifiedTime;
+
+    public URLContent(String cachedContent, long lastModifiedTime) {
+        this.cachedContent = cachedContent;
+        this.lastModifiedTime = lastModifiedTime;
+    }
+
+    public String getCachedContent() {
+        return cachedContent;
+    }
+
+    public long getLastModifiedTime() {
+        return lastModifiedTime;
+    }
+
+}
\ No newline at end of file

_______________________________________________
Registry-dev mailing list
[email protected]
http://wso2.org/cgi-bin/mailman/listinfo/registry-dev

Reply via email to