You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by mt...@apache.org on 2009/10/03 08:16:51 UTC
svn commit: r821256 - in
/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime: ./
io/ net/ util/
Author: mturk
Date: Sat Oct 3 06:16:50 2009
New Revision: 821256
URL: http://svn.apache.org/viewvc?rev=821256&view=rev
Log:
Port StringManager from Tomcat
Added:
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Local.java (with props)
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/LocalStrings.properties (with props)
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Local.java (with props)
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/LocalStrings.properties (with props)
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/Local.java (with props)
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalStrings.properties (with props)
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/StringManager.java (with props)
Modified:
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Main.java
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/RandomAccessFile.java
Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Local.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Local.java?rev=821256&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Local.java (added)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Local.java Sat Oct 3 06:16:50 2009
@@ -0,0 +1,33 @@
+/*
+ * 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.commons.runtime;
+import org.apache.commons.runtime.util.StringManager;
+
+/** Package private constants
+ */
+class Local
+{
+
+ public static final String Package = "org.apache.commons.runtime";
+ public static final StringManager sm;
+
+ static {
+ sm = StringManager.getManager(Package);
+ }
+}
Propchange: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Local.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/LocalStrings.properties
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/LocalStrings.properties?rev=821256&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/LocalStrings.properties (added)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/LocalStrings.properties Sat Oct 3 06:16:50 2009
@@ -0,0 +1,16 @@
+# 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.
+
+runtime.unsupportedOs=Apache Commons Runtime does not support this operating system
Propchange: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/LocalStrings.properties
------------------------------------------------------------------------------
svn:eol-style = native
Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Main.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Main.java?rev=821256&r1=821255&r2=821256&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Main.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Main.java Sat Oct 3 06:16:50 2009
@@ -18,6 +18,7 @@
import org.apache.commons.runtime.exception.*;
import java.io.File;
+
/**
* Main class.
*
Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Local.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Local.java?rev=821256&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Local.java (added)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Local.java Sat Oct 3 06:16:50 2009
@@ -0,0 +1,33 @@
+/*
+ * 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.commons.runtime.io;
+import org.apache.commons.runtime.util.StringManager;
+
+/** Io package private constants
+ */
+class Local
+{
+
+ public static final String Package = "org.apache.commons.runtime.io";
+ public static final StringManager sm;
+
+ static {
+ sm = StringManager.getManager(Package);
+ }
+}
Propchange: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Local.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/LocalStrings.properties
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/LocalStrings.properties?rev=821256&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/LocalStrings.properties (added)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/LocalStrings.properties Sat Oct 3 06:16:50 2009
@@ -0,0 +1,16 @@
+# 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.
+
+file.EEXIST=File already exsts and CREATE and EXCL FileOpenMode was given
Propchange: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/LocalStrings.properties
------------------------------------------------------------------------------
svn:eol-style = native
Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/RandomAccessFile.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/RandomAccessFile.java?rev=821256&r1=821255&r2=821256&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/RandomAccessFile.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/RandomAccessFile.java Sat Oct 3 06:16:50 2009
@@ -21,6 +21,7 @@
import org.apache.commons.runtime.exception.ClosedDescriptorException;
import org.apache.commons.runtime.exception.AsyncClosedDescriptorException;
import org.apache.commons.runtime.exception.InvalidDescriptorException;
+import org.apache.commons.runtime.util.StringManager;
import java.io.Closeable;
import java.io.Flushable;
import java.io.FileNotFoundException;
@@ -54,10 +55,10 @@
}
/**
- * Flushed the underlying file by writing any buffered data.
+ * Flush the underlying file by writing any buffered data.
* <p>
- * {@code fsync()} transfers all modified in-core data of the object
- * referred to by {@code this} Descriptor to the disk device
+ * {@code fsync()} transfers all modified in-core data of the file object
+ * referred to by {@code this} file to the disk device
* (or other permanent storage device) where that object resides.
* The call blocks until the device reports that the transfer has
* completed. It also flushes metadata information associated with
@@ -73,14 +74,35 @@
fd.flush();
}
- public RandomAccessFile(File file, EnumSet<FileOpenMode> mode)
+ public RandomAccessFile(File file, EnumSet<FileOpenMode> mode)
throws FileNotFoundException, IllegalArgumentException, IOException,
SecurityException
{
fd = FileWrapper.open(file, mode);
if (fd == null) {
// File exists and EXCL mode was given
- // TODO: Throw something
+ throw new FileNotFoundException(Local.sm.get("file.EEXIST"));
}
}
+
+ public RandomAccessFile(File file, EnumSet<FileOpenMode> mode,
+ EnumSet<FileProtection> prot)
+ throws FileNotFoundException, IllegalArgumentException, IOException,
+ SecurityException
+ {
+ fd = FileWrapper.open(file, mode, prot);
+ if (fd == null) {
+ // File exists and EXCL mode was given
+ throw new FileNotFoundException(Local.sm.get("file.EEXIST"));
+ }
+ }
+
+ /** Create new random access file object from the {@code fd}.
+ */
+ public RandomAccessFile(Descriptor fd)
+ {
+ this.fd = fd;
+ }
+
+
}
Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/Local.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/Local.java?rev=821256&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/Local.java (added)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/Local.java Sat Oct 3 06:16:50 2009
@@ -0,0 +1,33 @@
+/*
+ * 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.commons.runtime.net;
+import org.apache.commons.runtime.util.StringManager;
+
+/** Net package private constants
+ */
+class Local
+{
+
+ public static final String Package = "org.apache.commons.runtime.net";
+ public static final StringManager sm;
+
+ static {
+ sm = StringManager.getManager(Package);
+ }
+}
Propchange: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/Local.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalStrings.properties
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalStrings.properties?rev=821256&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalStrings.properties (added)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalStrings.properties Sat Oct 3 06:16:50 2009
@@ -0,0 +1,14 @@
+# 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.
Propchange: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalStrings.properties
------------------------------------------------------------------------------
svn:eol-style = native
Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/StringManager.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/StringManager.java?rev=821256&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/StringManager.java (added)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/StringManager.java Sat Oct 3 06:16:50 2009
@@ -0,0 +1,143 @@
+/*
+ * 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.commons.runtime.util;
+
+import java.text.MessageFormat;
+import java.util.Hashtable;
+import java.util.Locale;
+import java.util.MissingResourceException;
+import java.util.ResourceBundle;
+
+/**
+ * An internationalization / localization helper class which reduces
+ * the bother of handling ResourceBundles and takes care of the
+ * common cases of message formating which otherwise require the
+ * creation of Object arrays and such.
+ *
+ * <p>
+ * The StringManager operates on a package basis. One StringManager
+ * per package can be created and accessed via the getManager method
+ * call.
+ * </p>
+ * <p>
+ * The StringManager will look for a ResourceBundle named by
+ * the package name given plus the suffix of "LocalStrings". In
+ * practice, this means that the localized information will be contained
+ * in a LocalStrings.properties file located in the package
+ * directory of the classpath.
+ * </p>
+ * <p>
+ * Please see the documentation for java.util.ResourceBundle for
+ * more information.
+ * </p>
+ */
+public class StringManager
+{
+
+ /**
+ * The ResourceBundle for this StringManager.
+ */
+ private ResourceBundle bundle;
+ private Locale locale;
+
+ /**
+ * Creates a new StringManager for a given package. This is a
+ * private method and all access to it is arbitrated by the
+ * static getManager method call so that only one StringManager
+ * per package will be created.
+ *
+ * @param packageName Name of package to create StringManager for.
+ */
+ private StringManager(String packageName)
+ {
+ String bundleName = packageName + ".LocalStrings";
+ bundle = ResourceBundle.getBundle(bundleName, Locale.getDefault());
+ // Get the actual locale, which may be different from the requested one
+ locale = bundle.getLocale();
+ }
+
+ /**
+ * Get a string from the underlying resource bundle or return
+ * null if the String is not found.
+ *
+ * @param key to desired resource String
+ * @return resource String matching <i>key</i> from underlying
+ * bundle or null if not found.
+ * @throws IllegalArgumentException if <i>key</i> is null.
+ */
+ public String get(final String key)
+ throws IllegalArgumentException
+ {
+ if (key == null) {
+ String msg = "key may not have a null value";
+ throw new IllegalArgumentException(msg);
+ }
+
+ String value = null;
+ try {
+ value = bundle.getString(key);
+ } catch (MissingResourceException mre) {
+ value = null;
+ }
+ return value;
+ }
+
+ /**
+ * Get a string from the underlying resource bundle and format
+ * it with the given set of arguments.
+ *
+ * @param key
+ * @param args
+ */
+ public String get(final String key, final Object... args)
+ throws IllegalArgumentException
+ {
+ String value = get(key);
+ if (value == null) {
+ value = key;
+ }
+
+ MessageFormat mf = new MessageFormat(value);
+ mf.setLocale(locale);
+ return mf.format(args, new StringBuffer(), null).toString();
+ }
+
+ // --------------------------------------------------------------
+ // STATIC SUPPORT METHODS
+ // --------------------------------------------------------------
+
+ private static Hashtable<String, StringManager> managers =
+ new Hashtable<String, StringManager>();
+
+ /**
+ * Get the StringManager for a particular package. If a manager for
+ * a package already exists, it will be reused, else a new
+ * StringManager will be created and returned.
+ *
+ * @param packageName The package name
+ */
+ public synchronized static StringManager getManager(String packageName)
+ {
+ StringManager mgr = managers.get(packageName);
+ if (mgr == null) {
+ mgr = new StringManager(packageName);
+ managers.put(packageName, mgr);
+ }
+ return mgr;
+ }
+}
Propchange: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/StringManager.java
------------------------------------------------------------------------------
svn:eol-style = native