Beispiel #1
0
  /**
   * 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();
    }
  }
Beispiel #2
0
 private static void _runGc() {
   long usedMem1 = usedMemory();
   long usedMem2 = Long.MAX_VALUE;
   for (int i = 0; usedMem1 < usedMem2 && i < 500; i++) {
     runtime.runFinalization();
     runtime.gc();
     Thread.currentThread().yield();
     usedMem2 = usedMem1;
     usedMem1 = usedMemory();
   }
 }
Beispiel #3
0
  /**
   * 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;
    }
  }
Beispiel #4
0
  /**
   * Called when the users asks to show or hide all known information about a certain object from
   * the database.
   *
   * @param user User ID
   * @param anchor Unique ID of Anchor
   * @param show If true, show more information, if false, hide information
   * @param type Session type (edit, query, browse)
   * @param key browsing session id (null if session type is EDIT)
   * @return AnchorInfo[] with serialisable version of feedback text, empty if the user's session is
   *     expired or null if an error occurred
   */
  public AnchorInfo[] changeTextContent(
      String user, String anchor, boolean show, int type, String key) {
    if (!userMap.containsKey(user)) return new AnchorInfo[0];

    LiberSession session = userMap.get(user);

    Runtime r = Runtime.getRuntime();
    r.gc(); // FORCES GARBAGE COLLECTION
    r.runFinalization();

    synchronized (session) {
      session.setLastUpdated();
      return session.changeTextContent(anchor, show, type, key);
    }
  }
Beispiel #5
0
 private void gc() {
   Runtime rt = Runtime.getRuntime();
   for (int i = 0; i < 3; i++) {
     try {
       allocateMemory(1000000);
     } catch (Throwable th) {
     }
     for (int j = 0; j < 3; j++) rt.gc();
   }
   rt.runFinalization();
   try {
     Thread.sleep(50);
   } catch (Throwable th) { //
   }
 }
Beispiel #6
0
  /**
   * Ends this particular session. If it's a browsing session, the browsing generator is removed;
   * otherwise, the entire user's session can be removed.
   *
   * @param user user id
   * @param type type of session
   * @param key browsing session id (null if session type is EDIT)
   */
  public void endSession(String user, int type, String key) {
    if (!userMap.containsKey(user)) return;

    if (type == LiberSession.BROWSE) {
      LiberSession session = userMap.get(user);
      synchronized (session) {
        session.removeBrowsingGenerator(key);
      }
    } else {
      if (type == LiberSession.QUERY) userMap.get(user).printQueryTimes();
      userMap.remove(user);
    }

    Runtime r = Runtime.getRuntime();
    r.gc(); // FORCES GARBAGE COLLECTION
    r.runFinalization();
  }
Beispiel #7
0
  /**
   * 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];
    }
  }
Beispiel #8
0
  protected static long runGCUsedMemory() {
    Runtime runtime = Runtime.getRuntime();
    long usedMem1 = runtime.totalMemory() - runtime.freeMemory();
    long usedMem2 = Long.MAX_VALUE;
    int cnt = 4;
    while (cnt-- >= 0) {
      for (int i = 0; (usedMem1 < usedMem2) && (i < 1000); ++i) {
        // AVIAN FIXME
        runtime.runFinalization();
        runtime.gc();
        Thread.yield();

        usedMem2 = usedMem1;
        usedMem1 = runtime.totalMemory() - runtime.freeMemory();
      }
    }
    return usedMem1;
  }
Beispiel #9
0
  /**
   * 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;
    }
  }
  public static void main(String[] args) {
    Runtime rt = Runtime.getRuntime();
    long mem = rt.freeMemory();

    System.out.println(mem);

    String[] test = new String[10000];
    for (int i = 0; i < 10000; i++) {
      test[i] = new String("test");
    }
    mem = rt.freeMemory();

    System.out.println(mem);

    rt.runFinalization();
    rt.gc();
    mem = rt.freeMemory();

    System.out.println(mem);
  }
 public static void fullGC(boolean verbose) {
   if (verbose)
     System.out.print(
         new Date().toString()
             + ' '
             + String.valueOf((RUNTIME.totalMemory() - RUNTIME.freeMemory()) / 1024L)
             + "Kb used");
   long isFree = RUNTIME.freeMemory();
   long wasFree;
   do {
     wasFree = isFree;
     RUNTIME.runFinalization();
     RUNTIME.gc();
     isFree = RUNTIME.freeMemory();
   } while (isFree > wasFree);
   if (verbose)
     System.out.println(
         " --> "
             + String.valueOf((RUNTIME.totalMemory() - RUNTIME.freeMemory()) / 1024L)
             + "Kb used");
 }
Beispiel #12
0
  /**
   * 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];
    }
  }
Beispiel #13
0
 /**
  * 'Suggest' to run the garbage collector. Note this does not call the garbage collector
  * synchronously.
  */
 public static void gc() {
   rt.runFinalization();
   rt.gc();
 }
Beispiel #14
0
  /**
   * Creates a new AboutDialog object. The icon displayed in this dialog box is provided by the
   * parent shell. It should be either a unique image (Shell.getImage()), or the second image of a
   * list of (Shell.getImages()[1]).
   *
   * @param parent The parent shell (also carry the icon to display).
   * @param caption Caption text.
   * @param description Text for the application description line.
   * @param version Text for the application version line.
   */
  public AboutDialog(Shell parent, String caption, String description, String version) {
    // Take the opportunity to do some clean up if possible
    Runtime rt = Runtime.getRuntime();
    rt.runFinalization();
    rt.gc();

    shell = new Shell(parent, SWT.CLOSE | SWT.TITLE | SWT.RESIZE | SWT.APPLICATION_MODAL);
    shell.setText(caption);
    UIUtil.inheritIcon(shell, parent);
    shell.setLayout(new GridLayout());

    Composite cmpTmp = new Composite(shell, SWT.BORDER);
    cmpTmp.setLayoutData(new GridData(GridData.FILL_BOTH));
    GridLayout layTmp = new GridLayout(2, false);
    cmpTmp.setLayout(layTmp);

    // Application icon
    Label appIcon = new Label(cmpTmp, SWT.NONE);
    GridData gdTmp = new GridData();
    gdTmp.verticalSpan = 2;
    appIcon.setLayoutData(gdTmp);
    Image[] list = parent.getImages();
    // Gets the single icon
    if ((list == null) || (list.length < 2)) {
      appIcon.setImage(parent.getImage());
    } else { // Or the second one if there are more than one.
      appIcon.setImage(list[1]);
    }

    Label label = new Label(cmpTmp, SWT.NONE);
    label.setText(description == null ? "TBD" : description); // $NON-NLS-1$
    gdTmp =
        new GridData(
            GridData.HORIZONTAL_ALIGN_CENTER
                | GridData.GRAB_HORIZONTAL
                | GridData.VERTICAL_ALIGN_CENTER
                | GridData.GRAB_VERTICAL);
    label.setLayoutData(gdTmp);

    label = new Label(cmpTmp, SWT.NONE);
    label.setText(
        String.format(
            Res.getString("AboutDialog.versionLabel"),
            version == null ? "TBD" : version)); // $NON-NLS-1$
    gdTmp =
        new GridData(
            GridData.HORIZONTAL_ALIGN_CENTER
                | GridData.GRAB_HORIZONTAL
                | GridData.VERTICAL_ALIGN_CENTER
                | GridData.GRAB_VERTICAL);
    label.setLayoutData(gdTmp);

    // Info

    cmpTmp = new Composite(shell, SWT.BORDER);
    cmpTmp.setLayoutData(new GridData(GridData.FILL_BOTH));
    layTmp = new GridLayout(2, false);
    cmpTmp.setLayout(layTmp);

    label = new Label(cmpTmp, SWT.NONE);
    label.setText(Res.getString("AboutDialog.jvmVersion")); // $NON-NLS-1$
    label = new Label(cmpTmp, SWT.NONE);
    label.setText(System.getProperty("java.version")); // $NON-NLS-1$
    label = new Label(cmpTmp, SWT.NONE);
    label.setText(Res.getString("AboutDialog.platform")); // $NON-NLS-1$
    label = new Label(cmpTmp, SWT.NONE);
    label.setText(
        String.format(
            "%s, %s, %s", //$NON-NLS-1$
            System.getProperty("os.name"), // $NON-NLS-1$
            System.getProperty("os.arch"), // $NON-NLS-1$
            System.getProperty("os.version"))); // $NON-NLS-1$
    label = new Label(cmpTmp, SWT.NONE);
    label.setText(Res.getString("AboutDialog.memoryLabel")); // $NON-NLS-1$
    label = new Label(cmpTmp, SWT.NONE);
    NumberFormat nf = NumberFormat.getInstance();
    label.setText(
        String.format(
            Res.getString("AboutDialog.memory"), // $NON-NLS-1$
            nf.format(rt.freeMemory() / 1024),
            nf.format(rt.totalMemory() / 1024)));

    // --- Dialog-level buttons

    SelectionAdapter CloseActions =
        new SelectionAdapter() {
          public void widgetSelected(SelectionEvent e) {
            shell.close();
          };
        };
    ClosePanel pnlActions = new ClosePanel(shell, SWT.NONE, CloseActions, false);
    pnlActions.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    shell.setDefaultButton(pnlActions.btClose);

    shell.pack();
    shell.setMinimumSize(shell.getSize());
    Point startSize = shell.getMinimumSize();
    if (startSize.x < 350) startSize.x = 350;
    shell.setSize(startSize);
    Dialogs.centerWindow(shell, parent);
  }
Beispiel #15
0
 public static void runGC() {
   Runtime r = Runtime.getRuntime();
   r.runFinalization();
   r.gc();
 }