Skip to content

Commit

Permalink
NameRegistrar now internally uses a TSpace
Browse files Browse the repository at this point in the history
NameRegistrar now has a get(key,timeout) method.
  • Loading branch information
ar committed Jul 5, 2014
1 parent e900fe1 commit c5160f2
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 57 deletions.
4 changes: 2 additions & 2 deletions jpos/src/main/java/org/jpos/q2/cli/SHOWNR.java
Expand Up @@ -63,13 +63,13 @@ private void showAll(CLIContext cli, boolean detail)
{
NameRegistrar nr = NameRegistrar.getInstance();
int maxw = 0;
Iterator iter = nr.getMap().entrySet().iterator();
Iterator iter = nr.getAsMap().entrySet().iterator();
while (iter.hasNext())
{
Map.Entry entry = (Map.Entry) iter.next();
maxw = Math.max(maxw, entry.getKey().toString().length());
}
iter = nr.getMap().entrySet().iterator();
iter = nr.getAsMap().entrySet().iterator();
maxw++;
while (iter.hasNext())
{
Expand Down
2 changes: 1 addition & 1 deletion jpos/src/main/java/org/jpos/q2/cli/TAIL.java
Expand Up @@ -100,7 +100,7 @@ private void showLoggers(CLIContext cli)
{
NameRegistrar nr = NameRegistrar.getInstance();
int maxw = 0;
Iterator iter = nr.getMap().entrySet().iterator();
Iterator iter = nr.getAsMap().entrySet().iterator();
StringBuilder sb = new StringBuilder("available loggers:");
while (iter.hasNext())
{
Expand Down
2 changes: 1 addition & 1 deletion jpos/src/main/java/org/jpos/q2/cli/TMMON.java
Expand Up @@ -91,7 +91,7 @@ private void showTMs(CLIContext cli)
{
NameRegistrar nr = NameRegistrar.getInstance();
int maxw = 0;
Iterator iter = NameRegistrar.getMap().entrySet().iterator();
Iterator iter = NameRegistrar.getAsMap().entrySet().iterator();
StringBuilder sb = new StringBuilder("available transaction managers:");
while (iter.hasNext())
{
Expand Down
Expand Up @@ -34,7 +34,7 @@ public NameRegistrarInspector () {
}

public Map getRegistry () {
return NameRegistrar.getMap ();
return NameRegistrar.getAsMap();
}
}

4 changes: 2 additions & 2 deletions jpos/src/main/java/org/jpos/space/TSpace.java
Expand Up @@ -211,8 +211,8 @@ public synchronized void addListener (Object key, SpaceListener listener) {
public boolean isEmpty() {
return entries.isEmpty();
}
public Set getKeySet() {
return entries.keySet();
public synchronized Set<K> getKeySet() {
return new HashSet<K>(entries.keySet());
}
public String getKeysAsString () {
StringBuilder sb = new StringBuilder();
Expand Down
45 changes: 30 additions & 15 deletions jpos/src/main/java/org/jpos/util/NameRegistrar.java
Expand Up @@ -18,10 +18,12 @@

package org.jpos.util;

import org.jpos.space.SpaceUtil;
import org.jpos.space.TSpace;

import java.io.PrintStream;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
* Allow runtime binding of jPOS's components (ISOChannels, Logger, MUXes, etc)
Expand All @@ -31,7 +33,7 @@
*/
public class NameRegistrar implements Loggeable {
private static final NameRegistrar instance = new NameRegistrar();
private static final ConcurrentMap<String, Object> registrar = new ConcurrentHashMap<String, Object>();
private static final TSpace<String, Object> sp = new TSpace<String,Object>();

public static class NotFoundException extends Exception {
private static final long serialVersionUID = 8744022794646381475L;
Expand All @@ -49,8 +51,21 @@ private NameRegistrar() {
super();
}

public static ConcurrentMap<String, Object> getMap() {
return registrar;
public static TSpace<String, Object> getSpace() {
return sp;
}

/**
* @return a copy of the NameRegistrar's entries as a Map
*/
public static Map<String,Object> getAsMap() {
Map<String,Object> map = new HashMap<String,Object>();
for (String k : sp.getKeySet()) {
Object v = sp.rdp(k);
if (v != null)
map.put(k,v);
}
return map;
}

/**
Expand All @@ -69,15 +84,15 @@ public static NameRegistrar getInstance() {
* - value to be associated with the specified key
*/
public static void register(String key, Object value) {
getMap().put(key, value);
sp.put(key, value);
}

/**
* @param key
* key whose mapping is to be removed from registrar.
*/
public static void unregister(String key) {
getMap().remove(key);
SpaceUtil.wipe(sp, key);
}

/**
Expand All @@ -87,19 +102,23 @@ public static void unregister(String key) {
* if key not present in registrar
*/
public static Object get(String key) throws NotFoundException {
Object obj = getMap().get(key);
Object obj = sp.rdp(key);
if (obj == null) {
throw new NotFoundException(key);
}
return obj;
}

public static Object get(String key, long timeout) {
return sp.rd (key, timeout);
}

/**
* @param key
* key whose associated value is to be returned, null if not present.
*/
public static Object getIfExists(String key) {
return getMap().get(key);
return sp.rdp(key);
}

public void dump(PrintStream p, String indent) {
Expand All @@ -109,12 +128,8 @@ public void dump(PrintStream p, String indent) {
public void dump(PrintStream p, String indent, boolean detail) {
String inner = indent + " ";
p.println(indent + "name-registrar:");
for (Map.Entry<String, Object> entry : registrar.entrySet()) {
Object obj = entry.getValue();
String key = entry.getKey();
if (key == null) {
key = "null";
}
for (String key : sp.getKeySet()) {
Object obj = sp.rdp(key);
String objectClassName = (obj == null) ? "<NULL>" : obj.getClass().getName();
p.println(inner + key + ": " + objectClassName);
if (detail && obj instanceof Loggeable) {
Expand Down
40 changes: 20 additions & 20 deletions jpos/src/test/java/org/jpos/q2/qbean/QThreadPoolExecutorTest.java
@@ -1,21 +1,21 @@
/*
* jPOS Project [http://jpos.org]
* Copyright (C) 2000-2014 Alejandro P. Revilla
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

/*
* jPOS Project [http://jpos.org]
* Copyright (C) 2000-2014 Alejandro P. Revilla
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package org.jpos.q2.qbean;

import static org.fest.assertions.Assertions.assertThat;
Expand Down Expand Up @@ -92,12 +92,12 @@ public String getName() {
qbeanConfigBos = new ByteArrayOutputStream();
qbeanConfigPw = new PrintWriter(new OutputStreamWriter(qbeanConfigBos));

NameRegistrar.getMap().clear();
NameRegistrar.getAsMap().clear();
}

@After
public void tearDown() {
NameRegistrar.getMap().clear();
NameRegistrar.getAsMap().clear();
if (null != executor) {
try {
executor.shutdownNow();
Expand Down
Expand Up @@ -571,21 +571,6 @@ public void testSnapshotThrowsNullPointerException1() throws Throwable {
}
}

@Test
public void testStartServiceThrowsNullPointerException() throws Throwable {
try {
transactionManager.startService();
fail("Expected NullPointerException to be thrown");
} catch (NullPointerException ex) {
assertNull("ex.getMessage()", ex.getMessage());
assertNull("transactionManager.threads", transactionManager.threads);
assertNull("transactionManager.getConfiguration()", transactionManager.getConfiguration());
assertEquals("transactionManager.tail", 0L, transactionManager.tail);
assertNull("transactionManager.psp", transactionManager.psp);
assertNull("transactionManager.groups", transactionManager.groups);
}
}

@Test
public void testStartServiceThrowsNullPointerException1() throws Throwable {
transactionManager.setName("testTransactionManagerName");
Expand Down

0 comments on commit c5160f2

Please sign in to comment.