/** * Constructor. Initialises the servlet, reading the ontology model from static files and creating * a cleaner thread that removes obsolete sessions. */ public LiberServletImpl() { System.out.println("Starting up"); try { ontology = new OntologyReader(); // reads PolicyGrid ontologies // ontology = new GeographyOntologyReader(); //reads geography ontology clean = new TimerTask() { public void run() { // Removes obsolete sessions from the map, running every 10 min. long time = new GregorianCalendar().getTimeInMillis(); synchronized (userMap) { Iterator it = userMap.keySet().iterator(); List<String> obsolete = new ArrayList<String>(); while (it.hasNext()) { String key = (String) it.next(); long timeDif = time - userMap.get(key).getLastUpdated(); if ((timeDif < 0) || (timeDif > 3600000)) // if an hour has lapsed, or the next day has started obsolete.add(key); // (let's just hope no-one starts using the tool at midnight) } for (int i = 0; i < obsolete.size(); i++) // then remove that session userMap.remove(obsolete.get(i)); } } }; Timer timer = new Timer(); timer.schedule(clean, 0, 600000); Runtime r = Runtime.getRuntime(); r.gc(); // FORCES GARBAGE COLLECTION r.runFinalization(); } catch (Exception e) { e.printStackTrace(); } }
/** * Returns an NL-expression of the given date. * * @param user User ID * @param input QueryDateValue containing restriction on date * @return String with nl-expression for date restriction (e.g. 'before 2007'), empty String if * session expired or null if error occurred */ public String getDateExpression(String user, QueryDateValue input) { if (!userMap.containsKey(user)) return ""; try { return new SGDateNode(input.getDates()).getQueryNLExpression(input.getComparator()); } catch (Exception e) { e.printStackTrace(); return null; } }
/** * Creates a new session for the editing tool. * * @param user user id * @param project Project ID * @param resource Resource ID * @return String[] with user id and name */ public String[] newSession(String user, String project, String resource) { try { LiberSession session = new LiberSession(ontology, user, project, resource, LiberSession.EDIT); userMap.put(user, session); Runtime r = Runtime.getRuntime(); r.gc(); // FORCES GARBAGE COLLECTION r.runFinalization(); return session.getUserDetails(); } catch (Exception e) { e.printStackTrace(); return null; } }
/** * Returns the values of resourceID for the properties specified in formInformation. * * @param user User ID * @param resourceID Resource ID * @param formInformation FormInfo[] specifying properties * @return String[] with values, null if session expired or error occurred */ public String[] getDescriptionValues(String user, String resourceID, FormInfo[] formInformation) { try { if (!userMap.containsKey(user)) return null; LiberSession session = userMap.get(user); synchronized (session) { session.setLastUpdated(); return session.getDescriptionValues(resourceID, formInformation); } } catch (Exception e) { e.printStackTrace(); return null; } }
/** * Returns a tag cloud for the given property. * * @param user User ID * @param property Property name * @return TagCloud, null if session expired or error occurred */ public TagCloud getTagCloud(String user, String property) { try { if (!userMap.containsKey(user)) return null; LiberSession session = userMap.get(user); synchronized (session) { session.setLastUpdated(); TagCloud tc = new Folksonomy(user) .getTagCloud(session.getReader(), session.getReader().getProperty(property)); return tc; } } catch (Exception e) { e.printStackTrace(); return null; } }
/** * Initialises a new editing session. * * @param user user id * @param data Data provided about resource * @return AnchorInfo[] containing the feedback text. */ public AnchorInfo[] initSession(String user, InstanceData data) { try { LiberSession session = userMap.get(user); Runtime r = Runtime.getRuntime(); r.gc(); // FORCES GARBAGE COLLECTION r.runFinalization(); synchronized (session) { session.setLastUpdated(); return session.init(data); // initialise the session and return the text } } catch (Exception e) { e.printStackTrace(); return new AnchorInfo[0]; } }
/** * Returns an aggregated tag cloud for a set of properties. * * @param user User ID * @param properties String[] with property names * @return TagCloud, null if session expired or error occurred */ public TagCloud getTagCloud(String user, String[] properties) { try { if (!userMap.containsKey(user)) return null; LiberSession session = userMap.get(user); synchronized (session) { session.setLastUpdated(); List<OntProperty> list = new ArrayList<OntProperty>(); for (int i = 0; i < properties.length; i++) list.add(session.getReader().getProperty(properties[i])); TagCloud tc = new Folksonomy(user).getTagCloud(session.getReader(), list); return tc; } } catch (Exception e) { e.printStackTrace(); return null; } }
/** * Retrieves the values already added for this property and anchor * * @param user User ID * @param property Property name * @param anchor Unique ID of Anchor * @param key browsing session id (null if session type is EDIT) * @return String[] with values already added for this property and anchor, first element * --EXPIRED-- if session expired, null if error occurred */ public String[] getAddedValues( String user, String property, String anchor, int type, String key) { if (!userMap.containsKey(user)) { String[] result = new String[1]; result[0] = "---EXPIRED---"; return result; } try { LiberSession session = userMap.get(user); synchronized (session) { session.setLastUpdated(); return session.getAddedValues(property, anchor, type, key); } } catch (Exception e) { e.printStackTrace(); return null; } }
/** * Initialises a new query session, and returns the class hierarchy. This hierarchy is special * because it includes the number of instances of each type. * * @param user user id * @param roots root classes of the hierarchy (null to get complete hierarchy) * @return Hierarchy[] containing class hierarchy */ public Hierarchy[] initSessionAndGetClassHierarchy(String user, String[] roots) { try { if (userMap.containsKey(user)) userMap.get(user).setLastUpdated(); else userMap.put(user, new LiberSession(ontology, user, null, null, LiberSession.QUERY)); LiberSession session = userMap.get(user); Runtime r = Runtime.getRuntime(); r.gc(); // FORCES GARBAGE COLLECTION r.runFinalization(); synchronized (session) { return session.getCountedClassHierarchy(roots); } } catch (Exception e) { e.printStackTrace(); return null; } }
/** * Retrieves existing instances that qualify to be a target of this property, and instances that * already are a target of the property and anchor! Returns an array with first the range class * name, then those objects already in the range, then a 'null', and then all eligible objects. * * @param user user id * @param name property name * @param anchor Unique ID of Anchor * @param type session type * @param key browsing session id (null if session type is EDIT) * @return ExistingInstances, holds all necessary information about object that already are or * could be in the range, empty if session expired, or null if error occurred. */ public ExistingInstances getInstances( String user, String name, String anchor, int type, String key) { try { if (!userMap.containsKey(user)) { ExistingInstances result = new ExistingInstances(); result.setRange(null, null); return result; } LiberSession session = userMap.get(user); synchronized (session) { session.setLastUpdated(); ExistingInstances e = session.getRangeInstances(name, anchor, type, key); return e; } } catch (Exception e) { e.printStackTrace(); return null; } }
/** * Initialises a new browsing session, creating a new session for this user. * * @param user user id * @param resource Resource id * @param type Type of session (edit, query, browse) * @return AnchorInfo[] containing the feedback text. */ public AnchorInfo[] initSession(String user, String resource, int type) { try { if (type == LiberSession.BROWSE) // make a new user session if this is the browse tab userMap.put( user, new LiberSession(ontology, user, null, resource.replaceAll("\"", ""), type)); else if (!userMap.containsKey(user)) return null; LiberSession session = userMap.get(user); Runtime r = Runtime.getRuntime(); r.gc(); // FORCES GARBAGE COLLECTION r.runFinalization(); synchronized (session) { session.setLastUpdated(); return session.init( resource.replaceAll("\"", "")); // initialise the session and return the text } } catch (Exception e) { e.printStackTrace(); return new AnchorInfo[0]; } }