jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From thom...@apache.org
Subject svn commit: r1133300 - /jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/ExceptionFactory.java
Date Wed, 08 Jun 2011 09:22:55 GMT
Author: thomasm
Date: Wed Jun  8 09:22:54 2011
New Revision: 1133300

URL: http://svn.apache.org/viewvc?rev=1133300&view=rev
Log:
Exception factory (from jackrabbit-j3)

Added:
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/ExceptionFactory.java

Added: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/ExceptionFactory.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/ExceptionFactory.java?rev=1133300&view=auto
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/ExceptionFactory.java
(added)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/ExceptionFactory.java
Wed Jun  8 09:22:54 2011
@@ -0,0 +1,111 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.mk.mem;
+
+import java.text.MessageFormat;
+import java.util.ConcurrentModificationException;
+import java.util.HashMap;
+import java.util.NoSuchElementException;
+
+/**
+ * An exception factory.
+ */
+public class ExceptionFactory {
+
+    public static final int REPOSITORY_ALREADY_OPEN = 1002;
+    static final int CLOSED_0 = 1000;
+    static final int CONCURRENT_UPDATE_1 = 1001;
+
+    static final HashMap<Integer, String> MESSAGE_MAP = new HashMap<Integer, String>();
+
+
+    static {
+        MESSAGE_MAP.put(CLOSED_0, "The repository is closed");
+        MESSAGE_MAP.put(REPOSITORY_ALREADY_OPEN, "The repository is already open");
+    }
+
+    public static int getErrorCode(Exception e) {
+        String message = e.getMessage();
+        int open = message.lastIndexOf('[');
+        int dash = message.lastIndexOf('-');
+        if (dash < 0 || open < 0 || dash < open) {
+            return 0;
+        }
+        return Integer.parseInt(message.substring(open + 1, dash));
+    }
+
+    public static String getMessage(int errorCode, String... params) {
+        String message = MESSAGE_MAP.get(errorCode);
+        message = MessageFormat.format(message, (Object[]) params);
+        return message + " [" + errorCode + "-" + Constants.FULL_VERSION + "]";
+    }
+
+    static String format(String message, Object... params) {
+        if (params != null) {
+            message = MessageFormat.format(message, params);
+        }
+        return message + " [" + Constants.FULL_VERSION + "]";
+    }
+
+    public static <E extends Exception> E exception(Class<E> e, Throwable cause,
String msg, Object... params) throws E {
+        String message;
+        if (params != null && params.length > 0) {
+            message = format(msg, params);
+        } else {
+            message = msg == null ? cause.getMessage() : msg;
+        }
+        E i;
+        try {
+            i = e.getConstructor(String.class).newInstance(message);
+        } catch (Exception e1) {
+            throw new RuntimeException("Error building " + e + ": " + message, cause);
+        }
+        if (cause != null) {
+            i.initCause(cause);
+        }
+        throw i;
+    }
+
+    public static UnsupportedOperationException unsupportedOperation() {
+        throw exception(UnsupportedOperationException.class, null, "");
+    }
+
+    public static RuntimeException runtime(Exception e) {
+        throw exception(RuntimeException.class, e, e.getMessage());
+    }
+
+    public static IllegalArgumentException illegalArgument(String message, Object... params)
{
+        throw exception(IllegalArgumentException.class, null, message, params);
+    }
+
+    public static IllegalStateException illegalState(String message, Object... params) {
+        throw exception(IllegalStateException.class, null, message, params);
+    }
+
+    public static NullPointerException nullPointer() {
+        throw exception(NullPointerException.class, null, "");
+    }
+
+    public static ConcurrentModificationException concurrentModification() {
+        throw exception(ConcurrentModificationException.class, null, "");
+    }
+
+    public static NoSuchElementException noSuchElement() {
+        throw exception(NoSuchElementException.class, null, "");
+    }
+
+}



Mime
View raw message