[jboss-cvs] JBossAS SVN: r97193 - projects/logging-service-metadata/trunk/src/main/java/org/jboss/logging/metadata.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Mon Nov 30 20:02:25 EST 2009
Author: david.lloyd at jboss.com
Date: 2009-11-30 20:02:25 -0500 (Mon, 30 Nov 2009)
New Revision: 97193
Added:
projects/logging-service-metadata/trunk/src/main/java/org/jboss/logging/metadata/LogContextRegistry.java
Log:
Simple name-keyed log context registry
Added: projects/logging-service-metadata/trunk/src/main/java/org/jboss/logging/metadata/LogContextRegistry.java
===================================================================
--- projects/logging-service-metadata/trunk/src/main/java/org/jboss/logging/metadata/LogContextRegistry.java (rev 0)
+++ projects/logging-service-metadata/trunk/src/main/java/org/jboss/logging/metadata/LogContextRegistry.java 2009-12-01 01:02:25 UTC (rev 97193)
@@ -0,0 +1,83 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.logging.metadata;
+
+import org.jboss.util.collection.ConcurrentReferenceHashMap;
+import org.jboss.logmanager.LogContext;
+import static org.jboss.util.collection.ConcurrentReferenceHashMap.ReferenceType.*;
+import java.util.concurrent.ConcurrentMap;
+
+/**
+ * A named registry for log contexts. It retains only weak references to the log context instance.
+ */
+public final class LogContextRegistry {
+
+ private static final LogContextRegistry INSTANCE = new LogContextRegistry();
+
+ private final ConcurrentMap<String, LogContext> map = new ConcurrentReferenceHashMap<String, LogContext>(STRONG, WEAK);
+
+ private LogContextRegistry() {
+ map.put("system", LogContext.getSystemLogContext());
+ }
+
+ /**
+ * Get the named log context.
+ *
+ * @param name the context name
+ * @return the context
+ * @throws IllegalArgumentException if no such log context could be found
+ */
+ public LogContext get(String name) throws IllegalArgumentException {
+ final LogContext context = map.get(name);
+ if (context == null) {
+ throw new IllegalArgumentException("No such log context '" + name + "'");
+ }
+ return context;
+ }
+
+ /**
+ * Get the named log context, creating it if it does not exist.
+ *
+ * @param name the context name
+ * @return the context
+ */
+ public LogContext getOrCreate(String name) {
+ final LogContext context = map.get(name);
+ if (context == null) {
+ final LogContext created = LogContext.create();
+ final LogContext appearing = map.putIfAbsent(name, created);
+ return appearing == null ? created : appearing;
+ } else {
+ return context;
+ }
+ }
+
+ /**
+ * Get the single instance.
+ *
+ * @return the single instance
+ */
+ public static LogContextRegistry getInstance() {
+ return INSTANCE;
+ }
+}
More information about the jboss-cvs-commits
mailing list