@@ -14,6 +14,8 @@ |
import csip.Config.Registry; |
import csip.utils.JSONUtils; |
import csip.utils.Services; |
+import csip.utils.SimpleCache; |
+import java.util.function.Function; |
import java.util.logging.Level; |
import java.util.logging.Logger; |
|
@@ -34,6 +36,7 @@ |
public class CatalogService { |
|
static final Logger LOG = Logger.getLogger(CatalogService.class.getName()); |
+ static SimpleCache<Registry, String> cat = new SimpleCache<>(); |
|
|
@GET |
@@ -48,25 +51,48 @@ |
public String getJSONCatalog(@Context UriInfo uriInfo) { |
LOG.log(Level.INFO, "HTTP/GET {0}", uriInfo.getRequestUri().toString()); |
|
- JSONArray o = new JSONArray(); |
- try { |
- Registry r = Config.registry(); |
- String host = Services.toPublicURL(uriInfo.getRequestUri()).toString(); |
- if (!host.endsWith("/")) { |
- host += "/"; |
+ return cat.get(Config.registry(), (Registry r) -> { |
+ JSONArray o = new JSONArray(); |
+ try { |
+ String host = Services.toPublicURL(uriInfo.getRequestUri()).toString(); |
+ if (!host.endsWith("/")) { |
+ host += "/"; |
+ } |
+ for (Class<?> c : r.getServices()) { |
+ JSONObject m = new JSONObject(); |
+ String url = host + r.getServicePath(c); |
+ m.put(ModelDataService.KEY_NAME, r.getServiceName(c)); |
+ m.put(ModelDataService.KEY_DESC, r.getServiceDescription(c)); |
+ m.put(ModelDataService.KEY_URL, url); |
+ o.put(m); |
+ } |
+ return o.toString(2).replace("\\/", "/"); |
+ } catch (JSONException ex) { |
+ o.put(JSONUtils.error(ex.getMessage())); |
+ Logger.getLogger(CatalogService.class.getName()).log(Level.SEVERE, null, ex); |
} |
- for (Class<?> c : r.getServices()) { |
- JSONObject m = new JSONObject(); |
- String url = host + r.getServicePath(c); |
- m.put(ModelDataService.KEY_NAME, r.getServiceName(c)); |
- m.put(ModelDataService.KEY_DESC, r.getServiceDescription(c)); |
- m.put(ModelDataService.KEY_URL, url); |
- o.put(m); |
- } |
- return o.toString(2).replace("\\/", "/"); |
- } catch (JSONException ex) { |
- o.put(JSONUtils.error(ex.getMessage())); |
- } |
- return o.toString().replace("\\/", "/"); |
+ return o.toString().replace("\\/", "/"); |
+ }); |
+ |
+// JSONArray o = new JSONArray(); |
+// try { |
+// Registry r = Config.registry(); |
+// String host = Services.toPublicURL(uriInfo.getRequestUri()).toString(); |
+// if (!host.endsWith("/")) { |
+// host += "/"; |
+// } |
+// for (Class<?> c : r.getServices()) { |
+// JSONObject m = new JSONObject(); |
+// String url = host + r.getServicePath(c); |
+// m.put(ModelDataService.KEY_NAME, r.getServiceName(c)); |
+// m.put(ModelDataService.KEY_DESC, r.getServiceDescription(c)); |
+// m.put(ModelDataService.KEY_URL, url); |
+// o.put(m); |
+// } |
+// return o.toString(2).replace("\\/", "/"); |
+// } catch (JSONException ex) { |
+// o.put(JSONUtils.error(ex.getMessage())); |
+// } |
+// return o.toString().replace("\\/", "/"); |
} |
} |
@@ -11,6 +11,7 @@ |
*/ |
package csip; |
|
+import csip.utils.Services; |
import java.io.File; |
import java.io.FileInputStream; |
import java.io.FileNotFoundException; |
@@ -23,6 +24,7 @@ |
import javax.servlet.ServletContextEvent; |
import javax.servlet.ServletContextListener; |
import javax.servlet.annotation.WebListener; |
+import org.apache.commons.io.FileUtils; |
import org.apache.commons.io.IOUtils; |
import org.codehaus.jettison.json.JSONException; |
|
@@ -82,46 +84,39 @@ |
|
@Override |
public void contextInitialized(ServletContextEvent e) { |
- Config.startup(e.getServletContext()); |
- applyConfig(e.getServletContext(), "/WEB-INF/csip-defaults.json"); |
- applyConfig(e.getServletContext(), "/WEB-INF/csip-defaults.properties"); |
+ ServletContext ctx = e.getServletContext(); |
+ Config.startup(ctx); |
+ applyConfig(ctx, "/WEB-INF/csip-defaults.json"); |
+ applyConfig(ctx, "/WEB-INF/csip-defaults.properties"); |
+ applyConfig(ctx, "/META-INF/csip-conf.json"); // deprecated |
|
- applyConfig(e.getServletContext(), "/META-INF/csip-conf.json"); // deprecated |
+ applyContextInitParams(ctx); |
|
- applyContextInitParams(e.getServletContext()); |
- |
- String s = e.getServletContext().getRealPath("/WEB-INF"); |
- if (s != null) { |
- File file = new File(s).getParentFile(); |
- if (file != null && file.exists()) { |
- File c = new File(file.getParentFile(), file.getName() + ".json"); |
- if (c.exists()) { |
- try { |
- FileInputStream is = new FileInputStream(c); |
- ControlService.updateConfig(IOUtils.toString(is)); |
- l.info("Applied config '" + c + "'"); |
- } catch (FileNotFoundException ex) { |
- // ignore |
- } catch (JSONException | IOException ex) { |
- ex.printStackTrace(System.err); |
- } |
- } |
- c = new File(file.getParentFile(), file.getName() + ".properties"); |
- if (c.exists()) { |
- try { |
- Properties p = new Properties(); |
- p.load(new FileInputStream(c)); |
- ControlService.updateConfig(p); |
- l.info("Applied config '" + c + "'"); |
- } catch (FileNotFoundException ex) { |
- // ignore |
- } catch (IOException ex) { |
- ex.printStackTrace(System.err); |
- } |
- } |
+ File c = Services.getContextFile(ctx, ".json"); |
+ if (c.exists()) { |
+ try { |
+ ControlService.updateConfig(FileUtils.readFileToString(c)); |
+ l.info("Applied config '" + c + "'"); |
+ } catch (FileNotFoundException ex) { |
+ // ignore |
+ } catch (JSONException | IOException ex) { |
+ ex.printStackTrace(System.err); |
} |
} |
- l.info("CSIP Context Created: " + e.getServletContext().getContextPath()); |
+ c = Services.getContextFile(ctx, ".properties"); |
+ if (c.exists()) { |
+ try { |
+ Properties p = new Properties(); |
+ p.load(new FileInputStream(c)); |
+ ControlService.updateConfig(p); |
+ l.info("Applied config '" + c + "'"); |
+ } catch (FileNotFoundException ex) { |
+ // ignore |
+ } catch (IOException ex) { |
+ ex.printStackTrace(System.err); |
+ } |
+ } |
+ l.info("CSIP Context Created: " + ctx.getContextPath()); |
} |
|
|
@@ -22,6 +22,7 @@ |
import java.net.URI; |
import java.nio.ByteBuffer; |
import java.nio.channels.FileChannel; |
+import java.nio.charset.Charset; |
import java.security.MessageDigest; |
import java.text.SimpleDateFormat; |
import java.util.ArrayList; |
@@ -29,8 +30,10 @@ |
import java.util.Date; |
import java.util.Enumeration; |
import java.util.HashMap; |
+import java.util.HashSet; |
import java.util.List; |
import java.util.Map; |
+import java.util.Set; |
import java.util.TimeZone; |
import java.util.UUID; |
import java.util.concurrent.ArrayBlockingQueue; |
@@ -47,6 +50,8 @@ |
import java.util.concurrent.TimeUnit; |
import java.util.logging.Level; |
import java.util.logging.Logger; |
+import javax.servlet.ServletContext; |
+import javax.ws.rs.Path; |
import javax.ws.rs.client.Client; |
import javax.ws.rs.client.ClientBuilder; |
import javax.ws.rs.client.Entity; |
@@ -59,6 +64,7 @@ |
import org.apache.commons.compress.archivers.ArchiveStreamFactory; |
import org.apache.commons.compress.compressors.CompressorException; |
import org.apache.commons.compress.compressors.CompressorStreamFactory; |
+import org.apache.commons.io.FileUtils; |
import org.codehaus.jettison.json.JSONArray; |
import org.codehaus.jettison.json.JSONException; |
import org.codehaus.jettison.json.JSONObject; |
@@ -76,6 +82,8 @@ |
public static final String LOCAL_IP_ADDR = getLocalIP(); |
public static final int ENSEMBLE_THREADS = 10; |
|
+ static final Logger LOG = Logger.getLogger(Services.class.getName()); |
+ |
|
/** |
* Returns the current local IP address or an empty string in error case / |
@@ -264,7 +272,7 @@ |
int port = Config.getInt("csip.peer.port", 8080); |
UriBuilder b = UriBuilder.fromUri(uri); |
URI u = b.host(newHost).port(port).build(); |
- Logger.getLogger(Services.class.getName()).log(Level.INFO, "replace in Host: " + u.toString()); |
+ LOG.info("replace in Host: " + u.toString()); |
return u.toString(); |
} |
|
@@ -487,7 +495,6 @@ |
// executor.shutdown(); |
// return resp; |
// } |
- |
static synchronized ExecutorService getES(int nthreads, int bq_len) { |
BlockingQueue<Runnable> bq = new ArrayBlockingQueue<>(nthreads + bq_len); |
RejectedExecutionHandler eh = new ThreadPoolExecutor.CallerRunsPolicy(); |
@@ -682,4 +689,93 @@ |
return service.request(MediaType.APPLICATION_JSON).post(Entity.json(req), JSONObject.class); |
} |
} |
+ |
+ |
+ /** |
+ * get the a context peer file |
+ * |
+ * @param e |
+ * @param ext |
+ * @return |
+ */ |
+ public static File getContextFile(ServletContext e, String ext) { |
+ String s = e.getRealPath("/WEB-INF"); |
+ if (s != null) { |
+ File f = new File(s).getParentFile(); |
+ if (f != null && f.exists()) { |
+ File c = new File(f.getParentFile(), f.getName() + ext); |
+ if (c.exists() && c.canRead()) { |
+ return c; |
+ } |
+ } |
+ } |
+ return null; |
+ } |
+ |
+ |
+ public static void filterServiceResources(ServletContext c, Set<Class<?>> orig) { |
+ File serviceList = getContextFile(c, ".services"); |
+ // List to filter on. |
+ if (serviceList == null) { |
+ LOG.info("not found: " + serviceList + ", using all services."); |
+ return; |
+ } |
+ try { |
+ // read the file, a service for a line. |
+ List<String> srv = FileUtils.readLines(serviceList, "utf-8"); |
+ LOG.info("Register Services from: " + serviceList); |
+ |
+ // create a lookup table |
+ Map<String, Class<?>> smap = new HashMap<>(); |
+ for (Class<?> cl : orig) { |
+ Path p = cl.getAnnotation(Path.class); |
+ if (p != null) { |
+ String s = p.value(); |
+ if (s != null) { |
+ smap.put(s, cl); |
+ } |
+ } |
+ } |
+ |
+ Set<Class<?>> filtered_resources = new HashSet<>(); |
+ // add internal classes |
+ filtered_resources.add(org.glassfish.jersey.media.multipart.MultiPartFeature.class); |
+ for (Class<?> cl : orig) { |
+ if (cl.getCanonicalName().startsWith("csip.")) { |
+ filtered_resources.add(cl); |
+ } |
+ } |
+ int flen = filtered_resources.size(); |
+ for (String s : srv) { |
+ // allow for comment lines and empty lines. |
+ if (!s.isEmpty() && !s.trim().startsWith("#")) { |
+ String service = s.trim(); |
+ // be a bit more tolerant |
+ if (service.startsWith("/")) { |
+ service = service.substring(1); |
+ } |
+ Class<?> cl = smap.get(service); |
+ if (cl != null) { |
+ // you can only request model and data services to be added. |
+ // using package conventions |
+ if (cl.getCanonicalName().startsWith("m.") |
+ || cl.getCanonicalName().startsWith("d.")) { |
+ filtered_resources.add(cl); |
+ } |
+ } else { |
+ LOG.warning("service not found in " + serviceList + ": '" + s.trim() + "', ignoring."); |
+ } |
+ } |
+ } |
+ if (flen == filtered_resources.size()) { |
+ LOG.warning("No model or data service was enabled !!!!, check the file: "); |
+ } |
+ orig.clear(); |
+ orig.addAll(filtered_resources); |
+ } catch (IOException ex) { |
+ LOG.log(Level.WARNING, "cannot read: " + serviceList); |
+ LOG.info("Using all services."); |
+ } |
+ } |
+ |
} |