From 6d8d46dd5847e73a67e8677be8ed276fff3b16ab Mon Sep 17 00:00:00 2001 From: Chrriis Date: Thu, 3 May 2012 00:22:47 +0200 Subject: [PATCH] [#1177] Add SQL Console module to jOOQ - bug fix and code cleanup. --- .../remote/messaging/MessagingInterface.java | 2 +- .../remote/messaging/ObjectRegistry.java | 161 ------------------ 2 files changed, 1 insertion(+), 162 deletions(-) delete mode 100644 jOOQ-console/src/main/java/org/jooq/debug/console/remote/messaging/ObjectRegistry.java diff --git a/jOOQ-console/src/main/java/org/jooq/debug/console/remote/messaging/MessagingInterface.java b/jOOQ-console/src/main/java/org/jooq/debug/console/remote/messaging/MessagingInterface.java index fe20e7ec5a..e1e4715726 100644 --- a/jOOQ-console/src/main/java/org/jooq/debug/console/remote/messaging/MessagingInterface.java +++ b/jOOQ-console/src/main/java/org/jooq/debug/console/remote/messaging/MessagingInterface.java @@ -380,7 +380,7 @@ public class MessagingInterface { ThreadInfo threadInfo = new ThreadInfo(); boolean isAdded; synchronized (idToThreadInfo) { - isAdded = idToThreadInfo.put(threadID, threadInfo) != null; + isAdded = idToThreadInfo.put(threadID, threadInfo) == null; } CM_asyncExec asyncExec = new CM_asyncExec(); asyncExec.setArgs(threadID, message); diff --git a/jOOQ-console/src/main/java/org/jooq/debug/console/remote/messaging/ObjectRegistry.java b/jOOQ-console/src/main/java/org/jooq/debug/console/remote/messaging/ObjectRegistry.java deleted file mode 100644 index 8f2c2a2db9..0000000000 --- a/jOOQ-console/src/main/java/org/jooq/debug/console/remote/messaging/ObjectRegistry.java +++ /dev/null @@ -1,161 +0,0 @@ -/** - * Copyright (c) 2009-2012, Lukas Eder, lukas.eder@gmail.com - * Christopher Deckers, chrriis@gmail.com - * All rights reserved. - * - * This software is licensed to you under the Apache License, Version 2.0 - * (the "License"); You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * . Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * . Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * . Neither the name "jOOQ" nor the names of its contributors may be - * used to endorse or promote products derived from this software without - * specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ -package org.jooq.debug.console.remote.messaging; - -import java.lang.ref.WeakReference; -import java.util.HashMap; -import java.util.Map; - - -/** - * A convenient class to register objects to an ID. - * @author Christopher Deckers - */ -class ObjectRegistry { - - private Thread cleanUpThread; - - private synchronized void startThread() { - if(cleanUpThread != null) { - return; - } - cleanUpThread = new Thread("Registry cleanup thread") { - @Override - public void run() { - while(true) { - try { - sleep(5000); - } catch(Exception e) { - } - synchronized(ObjectRegistry.this) { - for(Integer instanceID: instanceIDToObjectReferenceMap.keySet().toArray(new Integer[0])) { - if(instanceIDToObjectReferenceMap.get(instanceID).get() == null) { - instanceIDToObjectReferenceMap.remove(instanceID); - } - } - if(instanceIDToObjectReferenceMap.isEmpty()) { - cleanUpThread = null; - return; - } - } - } - } - }; - cleanUpThread.setDaemon(true); - cleanUpThread.start(); - } - - private int nextInstanceID = 1; - private Map> instanceIDToObjectReferenceMap = new HashMap>(); - - /** - * Construct an object registry. - */ - public ObjectRegistry() { - } - - /** - * Add an object to the registry. - * @param o the object to add. - * @return an unused instance ID that is strictly greater than 0. - */ - public synchronized int add(Object o) { - while(true) { - int instanceID = nextInstanceID++; - if(!instanceIDToObjectReferenceMap.containsKey(instanceID)) { - if(o == null) { - return instanceID; - } - instanceIDToObjectReferenceMap.put(instanceID, new WeakReference(o)); - startThread(); - return instanceID; - } - } - } - - /** - * Add an object to the registry, specifying its ID, which throws an exception if the ID is already in use. - * @param o the object to add. - * @param instanceID the ID to associate the object to. - */ - public synchronized void add(Object o, int instanceID) { - Object o2 = get(instanceID); - if(o2 != null && o2 != o) { - throw new IllegalStateException("An object is already registered with the id \"" + instanceID + "\" for object: " + o); - } - instanceIDToObjectReferenceMap.put(instanceID, new WeakReference(o)); - startThread(); - } - - /** - * Get an object using its ID. - * @return the object, or null. - */ - public synchronized Object get(int instanceID) { - WeakReference weakReference = instanceIDToObjectReferenceMap.get(instanceID); - if(weakReference == null) { - return null; - } - Object o = weakReference.get(); - if(o == null) { - instanceIDToObjectReferenceMap.remove(instanceID); - } - return o; - } - - /** - * Remove an object from the registry using its instance ID. - * @param instanceID the ID of the object to remove. - */ - public synchronized void remove(int instanceID) { - instanceIDToObjectReferenceMap.remove(instanceID); - } - - /** - * Get all the instance IDs that are used in this registry. - * @return the instance IDs. - */ - public int[] getInstanceIDs() { - Object[] instanceIDObjects = instanceIDToObjectReferenceMap.keySet().toArray(); - int[] instanceIDs = new int[instanceIDObjects.length]; - for(int i=0; i