Permalink
Browse files

fix for server-side creation of the html page rather than working cli…

…ent side
  • Loading branch information...
atoulme committed Oct 29, 2010
1 parent b2c80bd commit 041c2f226905443804ade817e42662a02b204e50
@@ -25,7 +25,6 @@
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Collection;
-import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
@@ -58,26 +57,38 @@
*/
public class PluginServiceImpl implements IDiagramPluginService {
+ private static PluginServiceImpl _instance = null;
+
+ /**
+ * @param context the context needed for initialization
+ * @return the singleton of PluginServiceImpl
+ */
+ public static IDiagramPluginService getInstance(
+ ServletContext context) {
+ if (_instance == null) {
+ _instance = new PluginServiceImpl(context);
+ }
+ return _instance;
+ }
+
/**
- * The default local plugins, available to the webapp so that the default profile
- * can provision its plugins. Consumers through OSGi should use the service tracker
+ * The default local plugins, available to the webapp
+ * so that the default profile can provision its plugins.
+ * Consumers through OSGi should use the service tracker
* to get the plugins they need.
*/
private static Map<String, IDiagramPlugin> LOCAL = null;
/**
- * an object to lock over the local plugins registry.
- * Several servlets will have interest over the local plugin registry,
- * which requires the servlet context to initialize itself.
- * To avoid double initialization, we lock over this object.
+ * Initialize the local plugins registry
+ * @param context the servlet context necessary to grab
+ * the files inside the servlet.
+ * @return the set of local plugins organized by name
*/
- private static Object lock = new Object();
-
- public static Map<String, IDiagramPlugin> getLocalPluginsRegistry(ServletContext context) {
- synchronized(lock) {
- if (LOCAL == null) {
- LOCAL = initializeLocalPlugins(context);
- }
+ public static Map<String, IDiagramPlugin>
+ getLocalPluginsRegistry(ServletContext context) {
+ if (LOCAL == null) {
+ LOCAL = initializeLocalPlugins(context);
}
return LOCAL;
}
@@ -150,7 +161,12 @@
private Map<String, IDiagramPlugin> _registry = new HashMap<String, IDiagramPlugin>();
private Set<IDiagramPluginFactory> _factories = new HashSet<IDiagramPluginFactory>();
- public PluginServiceImpl(ServletContext context) {
+ /**
+ * Private constructor to make sure we respect the singleton
+ * pattern.
+ * @param context the servlet context
+ */
+ private PluginServiceImpl(ServletContext context) {
_registry.putAll(getLocalPluginsRegistry(context));
// if we are in the OSGi world:
if (getClass().getClassLoader() instanceof BundleReference) {
@@ -39,6 +39,7 @@
import org.slf4j.LoggerFactory;
import com.intalio.web.plugin.IDiagramPlugin;
+import com.intalio.web.plugin.IDiagramPluginService;
/**
* This servlet exposes the plugins registered against the platforms.
@@ -53,18 +54,22 @@
*/
public class PluginServiceServlet extends HttpServlet {
- private static final Logger _logger = LoggerFactory.getLogger(PluginServiceServlet.class);
+ private static final Logger _logger = LoggerFactory.getLogger(
+ PluginServiceServlet.class);
private static final long serialVersionUID = -2024110864538877629L;
- private PluginServiceImpl _pluginService;
+ private IDiagramPluginService _pluginService;
public void init(ServletConfig config) throws ServletException {
super.init(config);
- _pluginService = new PluginServiceImpl(config.getServletContext());
+ _pluginService = PluginServiceImpl.getInstance(
+ config.getServletContext());
}
- protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
+ protected void doGet(HttpServletRequest req,
+ HttpServletResponse resp)
+ throws ServletException, IOException {
try {
if ((EditorHandler.oryx_path + "plugins").equals(req.getRequestURI())) {
listAllPlugins(req, resp);
@@ -76,10 +81,12 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws Se
}
}
- private void retrievePluginContents(HttpServletRequest req, HttpServletResponse resp) {
+ private void retrievePluginContents(HttpServletRequest req,
+ HttpServletResponse resp) {
String name = req.getParameter("name");
if (name == null) {
- name = req.getRequestURI().substring(req.getRequestURI().lastIndexOf("/")+1);
+ name = req.getRequestURI().substring(
+ req.getRequestURI().lastIndexOf("/")+1);
if (name.endsWith(".js")) {
name = name.substring(0, name.length() - ".js".length());
}
@@ -89,11 +96,13 @@ private void retrievePluginContents(HttpServletRequest req, HttpServletResponse
}
IDiagramPlugin plugin = _pluginService.findPlugin(req, name);
if (plugin == null) {
- throw new IllegalArgumentException("No plugin by the name of " + name);
+ throw new IllegalArgumentException(
+ "No plugin by the name of " + name);
}
InputStream input = plugin.getContents();
if (input == null) {
- throw new IllegalArgumentException("Plugin contents could not be found");
+ throw new IllegalArgumentException(
+ "Plugin contents could not be found");
}
try {
byte[] buffer = new byte[4096];
@@ -105,20 +114,26 @@ private void retrievePluginContents(HttpServletRequest req, HttpServletResponse
} catch (IOException e) {
_logger.error(e.getMessage(), e);
} finally {
- if (input != null) { try { input.close(); } catch(IOException e) {}};
+ if (input != null) {
+ try { input.close(); } catch(IOException e) {}
+ }
}
}
- private void listAllPlugins(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException {
+ private void listAllPlugins(HttpServletRequest req,
+ HttpServletResponse resp)
+ throws IOException, JSONException {
JSONArray plugins = new JSONArray();
- for (IDiagramPlugin p : _pluginService.getRegisteredPlugins(req)) {
+ for (IDiagramPlugin p :
+ _pluginService.getRegisteredPlugins(req)) {
JSONObject obj = new JSONObject();
obj.put("name", p.getName());
obj.put("core", p.isCore());
JSONArray properties = new JSONArray();
if (p.getProperties() != null) {
- for (Entry<String, Object> entry : p.getProperties().entrySet()) {
+ for (Entry<String, Object> entry :
+ p.getProperties().entrySet()) {
JSONObject propObj = new JSONObject();
propObj.put(entry.getKey(), entry.getValue());
properties.put(propObj);
@@ -50,67 +50,97 @@
*/
public class ProfileServiceImpl implements IDiagramProfileService {
- private Map<String, IDiagramProfile> _registry = new HashMap<String, IDiagramProfile>();
- private Set<IDiagramProfileFactory> _factories = new HashSet<IDiagramProfileFactory>();
+ /**
+ * the singleton instance
+ */
+ private static IDiagramProfileService _instance;
- public ProfileServiceImpl(ServletContext context) {
+ public static IDiagramProfileService getInstance(
+ ServletContext context) {
+ if (_instance == null) {
+ _instance = new ProfileServiceImpl(context);
+ }
+ return _instance;
+ }
+
+ private Map<String, IDiagramProfile> _registry =
+ new HashMap<String, IDiagramProfile>();
+ private Set<IDiagramProfileFactory> _factories =
+ new HashSet<IDiagramProfileFactory>();
+
+ /**
+ * Default constructor, private.
+ * @param context the servlet context to initialize the profile.
+ */
+ private ProfileServiceImpl(ServletContext context) {
_registry.put("default", new DefaultProfileImpl(context));
// if we are in the OSGi world:
if (getClass().getClassLoader() instanceof BundleReference) {
- final BundleContext bundleContext = ((BundleReference) getClass().getClassLoader()).getBundle().getBundleContext();
- ServiceReference[] sRefs = null;
- try {
- sRefs = bundleContext.getServiceReferences(IDiagramProfileFactory.class.getName(), null);
- } catch (InvalidSyntaxException e) {
- }
- if (sRefs != null) {
- for (ServiceReference sRef : sRefs) {
- IDiagramProfileFactory service = (IDiagramProfileFactory) bundleContext.getService(sRef);
- _factories.add(service);
- }
+ registerWithOSGiContext();
+ }
+ }
+
+ /**
+ * register the profile and look for
+ * factories in an OSGi context.
+ */
+ private void registerWithOSGiContext() {
+ BundleReference ref = (BundleReference)
+ getClass().getClassLoader();
+ final BundleContext bundleContext =
+ ref.getBundle().getBundleContext();
+ ServiceReference[] sRefs = null;
+ try {
+ sRefs = bundleContext.getServiceReferences(
+ IDiagramProfileFactory.class.getName(), null);
+ } catch (InvalidSyntaxException e) {
+ }
+ if (sRefs != null) {
+ for (ServiceReference sRef : sRefs) {
+ IDiagramProfileFactory service = (IDiagramProfileFactory) bundleContext.getService(sRef);
+ _factories.add(service);
}
- ServiceTrackerCustomizer cust = new ServiceTrackerCustomizer() {
+ }
+ ServiceTrackerCustomizer cust = new ServiceTrackerCustomizer() {
- public void removedService(ServiceReference reference, Object service) {
- }
+ public void removedService(ServiceReference reference, Object service) {
+ }
- public void modifiedService(ServiceReference reference, Object service) {
- }
+ public void modifiedService(ServiceReference reference, Object service) {
+ }
- public Object addingService(ServiceReference reference) {
- IDiagramProfileFactory service = (IDiagramProfileFactory) bundleContext.getService(reference);
- _factories.add(service);
- return service;
- }
- };
- ServiceTracker tracker = new ServiceTracker(bundleContext,
- IDiagramProfileFactory.class.getName(), cust);
- tracker.open();
- // register self to make the default profile available to the world:
- bundleContext.registerService(IDiagramProfileService.class.getName(), this, new Hashtable());
- }
+ public Object addingService(ServiceReference reference) {
+ IDiagramProfileFactory service = (IDiagramProfileFactory) bundleContext.getService(reference);
+ _factories.add(service);
+ return service;
+ }
+ };
+ ServiceTracker tracker = new ServiceTracker(bundleContext,
+ IDiagramProfileFactory.class.getName(), cust);
+ tracker.open();
+ // register self to make the default profile available to the world:
+ bundleContext.registerService(IDiagramProfileService.class.getName(), this, new Hashtable());
}
- private Map<String, IDiagramProfile> assemblePlugins(HttpServletRequest request) {
- Map<String, IDiagramProfile> plugins = new HashMap<String, IDiagramProfile>(_registry);
+ private Map<String, IDiagramProfile> assembleProfiles(HttpServletRequest request) {
+ Map<String, IDiagramProfile> profiles = new HashMap<String, IDiagramProfile>(_registry);
if (request != null) {
for (IDiagramProfileFactory factory : _factories) {
for (IDiagramProfile p : factory.getProfiles(request)) {
- plugins.put(p.getName(), p);
+ profiles.put(p.getName(), p);
}
}
}
- System.err.println(plugins.keySet());
- return plugins;
+ return profiles;
}
public IDiagramProfile findProfile(HttpServletRequest request, String name) {
- return assemblePlugins(request).get(name);
+ return assembleProfiles(request).get(name);
}
public Collection<IDiagramProfile> getProfiles(HttpServletRequest request) {
- return assemblePlugins(request).values();
+ return assembleProfiles(request).values();
}
@@ -1,81 +0,0 @@
-/***************************************
- * Copyright (c) Intalio, Inc 2010
- *
- * Permission is hereby granted, free of charge, to any person obtaining a
- * copy of this software and associated documentation files (the "Software"),
- * to deal in the Software without restriction, including without limitation
- * the rights to use, copy, modify, merge, publish, distribute, sublicense,
- * and/or sell copies of the Software, and to permit persons to whom the
- * Software is furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
- * DEALINGS IN THE SOFTWARE.
-****************************************/
-package com.intalio.web.profile.impl;
-
-import java.io.IOException;
-
-import javax.servlet.ServletConfig;
-import javax.servlet.ServletException;
-import javax.servlet.http.HttpServlet;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-
-import org.json.JSONArray;
-import org.json.JSONException;
-import org.json.JSONObject;
-
-import com.intalio.web.plugin.IDiagramPlugin;
-import com.intalio.web.plugin.IDiagramPluginService;
-import com.intalio.web.profile.IDiagramProfile;
-
-/**
- * The profile service servlet, to make profiles available to the editor.
- *
- * @author Antoine Toulme
- */
-public class ProfileServiceServlet extends HttpServlet {
-
- private static final long serialVersionUID = 8234640848580375543L;
-
- private ProfileServiceImpl _profileService;
-
- public void init(ServletConfig config) throws ServletException {
- super.init(config);
- _profileService = new ProfileServiceImpl(config.getServletContext());
- }
-
- protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- String name = req.getParameter("name");
- if (name == null) {
- throw new IllegalArgumentException("Name parameter required");
- }
-
- IDiagramProfile p = _profileService.findProfile(req, name);
- if (p == null) {
- throw new IllegalArgumentException("No profile by the name of " + name);
- }
- try {
- JSONObject profile = new JSONObject();
- profile.put("name", p.getName());
- profile.put("title", p.getTitle());
- profile.put("plugins", p.getPlugins());
- profile.put("stencilset", p.getStencilSet());
- profile.put("ssexts", p.getStencilSetExtensions());
- resp.getWriter().append(profile.toString());
- } catch (JSONException e) {
- throw new ServletException(e);
- }
-
-
-
- }
-}
Oops, something went wrong.

0 comments on commit 041c2f2

Please sign in to comment.