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 2011/04/13 20:20:02 UTC
svn commit: r1091867 - in /commons/sandbox/runtime/trunk/src/main:
java/org/apache/commons/runtime/ java/org/apache/commons/runtime/exception/
java/org/apache/commons/runtime/platform/linux/
java/org/apache/commons/runtime/platform/windows/ native/shared/
Author: mturk
Date: Wed Apr 13 18:20:01 2011
New Revision: 1091867
URL: http://svn.apache.org/viewvc?rev=1091867&view=rev
Log:
Add Status and few exception classes
Added:
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Status.java (with props)
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/exception/ClosedDescriptorException.java (with props)
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/exception/InvalidDescriptorException.java (with props)
Modified:
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/PosixSemaphore.java
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/exception/AlreadyExistsException.java
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/exception/NoSuchObjectException.java
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/exception/UnsupportedOperatingSystemException.java
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/linux/SELinux.java
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/RegistryKey.java
commons/sandbox/runtime/trunk/src/main/native/shared/error.c
Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/PosixSemaphore.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/PosixSemaphore.java?rev=1091867&r1=1091866&r2=1091867&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/PosixSemaphore.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/PosixSemaphore.java Wed Apr 13 18:20:01 2011
@@ -18,6 +18,7 @@ package org.apache.commons.runtime;
import java.io.IOException;
import org.apache.commons.runtime.exception.AlreadyExistsException;
import org.apache.commons.runtime.exception.NoSuchObjectException;
+import org.apache.commons.runtime.exception.ClosedDescriptorException;
import org.apache.commons.runtime.exception.SystemException;
/**
@@ -53,6 +54,149 @@ final class PosixSemaphore extends Semap
private static native int release0(long sema);
// OS semaphore handle (sem_t)
- private long sd;
+ private long sd;
+ private String name;
+ private boolean owner;
+
+ public PosixSemaphore(final String name, int value)
+ throws IllegalAccessException,
+ IllegalArgumentException,
+ AlreadyExistsException,
+ SystemException
+ {
+ if (name == null)
+ throw new NullPointerException();
+ this.name = "/" + name.replace('/', '_');
+
+ while (true) {
+ try {
+ sd = create0(this.name, value);
+ break;
+ }
+ catch (IllegalArgumentException e) {
+ try {
+ this.name = this.name.substring(0, 13);
+ }
+ catch (IndexOutOfBoundsException x) {
+ throw e;
+ }
+ }
+ }
+ owner = true;
+ }
+
+ public PosixSemaphore(final String name)
+ throws IllegalAccessException,
+ IllegalArgumentException,
+ NoSuchObjectException,
+ SystemException
+ {
+ if (name == null)
+ throw new NullPointerException();
+ this.name = "/" + name.replace('/', '_');
+
+ sd = open0(this.name);
+ owner = false;
+ }
+
+ public void acquire()
+ throws SystemException
+ {
+ if (sd == 0L)
+ throw new ClosedDescriptorException();
+ int rc = wait0(sd);
+ if (rc != 0)
+ throw new SystemException(Status.describe(rc));
+ }
+
+ public boolean tryAcquire()
+ throws SystemException
+ {
+ if (sd == 0L)
+ throw new ClosedDescriptorException();
+ int rc = try0(sd);
+ if (rc == 0)
+ return true;
+ if (Status.IS_EBUSY(rc))
+ return false;
+ throw new SystemException(Status.describe(rc));
+ }
+
+ public void release()
+ throws SystemException
+ {
+ if (sd == 0L)
+ throw new ClosedDescriptorException();
+ int rc = release0(sd);
+ if (rc != 0)
+ throw new SystemException(Status.describe(rc));
+ }
+
+ public void clear()
+ throws SystemException
+ {
+ boolean once = false;
+
+ if (sd == 0L)
+ throw new ClosedDescriptorException();
+ while (true) {
+ int rc = try0(sd);
+ if (rc == 0) {
+ once = true;
+ }
+ else {
+ if (once) {
+ // We already obtained a lock once.
+ break;
+ }
+ else {
+ // Not an owner
+ throw new SystemException(Status.describe(rc));
+ }
+ }
+ }
+ }
+
+ public void close()
+ throws SystemException
+ {
+ int rc;
+ if (sd == 0L)
+ throw new ClosedDescriptorException();
+ if (owner) {
+ // Unlink if we are the semaphore owner.
+ rc = unlink0(name);
+ if (rc != 0)
+ throw new SystemException(Status.describe(rc));
+ }
+ rc = close0(sd);
+ if (rc != 0)
+ throw new SystemException(Status.describe(rc));
+ sd = 0L;
+ }
+
+ public String getCanonicalName()
+ {
+ return name;
+ }
+
+ /**
+ * Called by the garbage collector when the object is destroyed.
+ * The class will free internal resources allocated by the Operating system.
+ * @see Object#finalize()
+ * @throws Throwable the {@code Exception} raised by this method.
+ */
+ @Override
+ protected final void finalize()
+ throws Throwable
+ {
+ if (owner) {
+ // Unlink if we are the semaphore owner.
+ unlink0(name);
+ }
+ if (sd != 0L) {
+ close0(sd);
+ }
+ }
}
Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Status.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Status.java?rev=1091867&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Status.java (added)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Status.java Wed Apr 13 18:20:01 2011
@@ -0,0 +1,157 @@
+/* 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;
+
+/**
+ * Status codes.
+ *
+ * @since Runtime 1.0
+ *
+ */
+public class Status
+{
+
+ private Status()
+ {
+ // No class instance
+ }
+
+ /* Hard coded definitions for acr/error.h
+ */
+ private static final int OS_START_ERROR = 200000;
+ private static final int OS_START_STATUS = 300000;
+ /**
+ * ACR_OS_START_CANONERR is where ACR versions of errno values are defined
+ * on systems which don't have the corresponding errno.
+ */
+ private static final int OS_START_CANONERR = 500000;
+ private static final int OS_LAST_CANONERR = 500091;
+ /**
+ * ACR_OS_ERRSPACE_SIZE is the maximum number of errors you can fit
+ * into one of the error/status ranges below -- except for
+ * ACR_OS_START_USERERR, which see.
+ */
+ private static final int OS_ERRSPACE_SIZE = 50000;
+
+ /** Status is OK. */
+ public static final int OK = 0;
+ /** Unable to perform a stat on the file. */
+ public static final int ENOSTAT = OS_START_ERROR + 1;
+ /** Pool from which to allocate the memory was not provided. */
+ public static final int ENOPOOL = OS_START_ERROR + 2;
+ /* empty slot: +3 */
+ /** Date is invalid */
+ public static final int EBADDATE = OS_START_ERROR + 4;
+ /** Socket is invalid */
+ public static final int EINVALSOCK = OS_START_ERROR + 5;
+ /** Invalid process structure */
+ public static final int ENOPROC = OS_START_ERROR + 6;
+ /** Invalid time structure */
+ public static final int ENOTIME = OS_START_ERROR + 7;
+ /** Invalid directory structure */
+ public static final int ENODIR = OS_START_ERROR + 8;
+ /** Invalid lock structure */
+ public static final int ENOLOCK = OS_START_ERROR + 9;
+ /** Invalid poll structure */
+ public static final int ENOPOLL = OS_START_ERROR + 10;
+ /** Given data is not a socket */
+ public static final int ENOSOCKET = OS_START_ERROR + 11;
+ /** Given data is not a thread */
+ public static final int ENOTHREAD = OS_START_ERROR + 12;
+ /** Given data is not a thread key */
+ public static final int ENOTHDKEY = OS_START_ERROR + 13;
+ /* General failure <em>specific information not available</em>. */
+ public static final int EGENERAL = OS_START_ERROR + 14;
+ /** The specified IP address is invalid */
+ public static final int EBADIP = OS_START_ERROR + 15;
+ /** The specified netmask is invalid */
+ public static final int EBADMASK = OS_START_ERROR + 16;
+ /** No more shared memory available */
+ public static final int ENOSHMAVAIL = OS_START_ERROR + 17;
+ /** Unable to open the {@code dso} object. */
+ /* empty slot: +18 */
+ public static final int EDSOOPEN = OS_START_ERROR + 19;
+ /** Could not find the requested symbol */
+ public static final int ESYMNOTFOUND = OS_START_ERROR + 26;
+ /** Not enough entropy to continue */
+ public static final int ENOTENOUGHENTROPY = OS_START_ERROR + 28;
+
+ /* ACR private errors added to the std APR errors */
+ public static final int EISNULL = OS_START_ERROR + 100;
+ public static final int EINVALSIZ = OS_START_ERROR + 101;
+ public static final int ERDONLY = OS_START_ERROR + 102;
+ public static final int ECLASSNOTFOUND = OS_START_ERROR + 103;
+ public static final int ENOJNIENV = OS_START_ERROR + 104;
+
+ public static final int INCHILD = OS_START_STATUS + 1;
+ public static final int INPARENT = OS_START_STATUS + 2;
+ public static final int DETACH = OS_START_STATUS + 3;
+ public static final int NOTDETACH = OS_START_STATUS + 4;
+ public static final int CHILD_DONE = OS_START_STATUS + 5;
+ public static final int CHILD_NOTDONE = OS_START_STATUS + 6;
+ public static final int TIMEUP = OS_START_STATUS + 7;
+ public static final int INCOMPLETE = OS_START_STATUS + 8;
+ /* empty slot: +9 */
+ /* empty slot: +10 */
+ /* empty slot: +11 */
+ public static final int BADCH = OS_START_STATUS + 12;
+ public static final int BADARG = OS_START_STATUS + 13;
+ public static final int EOF = OS_START_STATUS + 14;
+ public static final int NOTFOUND = OS_START_STATUS + 15;
+ /* empty slot: +16 */
+ /* empty slot: +17 */
+ /* empty slot: +18 */
+ public static final int ANONYMOUS = OS_START_STATUS + 19;
+ public static final int FILEBASED = OS_START_STATUS + 20;
+ public static final int KEYBASED = OS_START_STATUS + 21;
+ public static final int EINIT = OS_START_STATUS + 22;
+ public static final int ENOTIMPL = OS_START_STATUS + 23;
+ public static final int EMISMATCH = OS_START_STATUS + 24;
+
+ public static boolean IS_STATUS(int s)
+ {
+ return ((s > OS_START_STATUS) && (s < OS_START_STATUS + OS_ERRSPACE_SIZE));
+ }
+
+ private static native boolean is0(int what, int err);
+
+ /** Permission denied */
+ public static boolean IS_EACCES(int s)
+ {
+ return is0(0, s);
+ }
+
+ /** File exists */
+ public static boolean IS_EEXIST(int s)
+ {
+ return is0(1, s);
+ }
+
+ /** File does not exists */
+ public static boolean IS_ENOENT(int s)
+ {
+ return is0(2, s);
+ }
+
+ /** Resource is busy */
+ public static boolean IS_EBUSY(int s)
+ {
+ return is0(3, s);
+ }
+
+ public static native String describe(int s);
+}
Propchange: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Status.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/exception/AlreadyExistsException.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/exception/AlreadyExistsException.java?rev=1091867&r1=1091866&r2=1091867&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/exception/AlreadyExistsException.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/exception/AlreadyExistsException.java Wed Apr 13 18:20:01 2011
@@ -24,7 +24,7 @@ package org.apache.commons.runtime.excep
* @since Runtime 1.0
*/
-public class AlreadyExistsException extends RuntimeException
+public class AlreadyExistsException extends SystemException
{
public AlreadyExistsException()
Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/exception/ClosedDescriptorException.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/exception/ClosedDescriptorException.java?rev=1091867&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/exception/ClosedDescriptorException.java (added)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/exception/ClosedDescriptorException.java Wed Apr 13 18:20:01 2011
@@ -0,0 +1,38 @@
+/* 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.exception;
+import java.io.IOException;
+
+/**
+ * ClosedDescriptorException thrown when an attempt is made to
+ * invoke an operation on closed operating system {@code descriptor}.
+ *
+ * @since Runtime 1.0
+ */
+public class ClosedDescriptorException extends SystemException
+{
+
+ public ClosedDescriptorException()
+ {
+ super();
+ }
+
+ public ClosedDescriptorException(String msg)
+ {
+ super(msg);
+ }
+}
Propchange: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/exception/ClosedDescriptorException.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/exception/InvalidDescriptorException.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/exception/InvalidDescriptorException.java?rev=1091867&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/exception/InvalidDescriptorException.java (added)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/exception/InvalidDescriptorException.java Wed Apr 13 18:20:01 2011
@@ -0,0 +1,38 @@
+/* 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.exception;
+import java.io.IOException;
+
+/**
+ * InvalidDescriptorException thrown when an attempt is made to
+ * invoke an operation on invalid {@code descriptor}.
+ *
+ * @since Runtime 1.0
+ */
+public class InvalidDescriptorException extends SystemException
+{
+
+ public InvalidDescriptorException()
+ {
+ super();
+ }
+
+ public InvalidDescriptorException(String msg)
+ {
+ super(msg);
+ }
+}
Propchange: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/exception/InvalidDescriptorException.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/exception/NoSuchObjectException.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/exception/NoSuchObjectException.java?rev=1091867&r1=1091866&r2=1091867&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/exception/NoSuchObjectException.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/exception/NoSuchObjectException.java Wed Apr 13 18:20:01 2011
@@ -24,7 +24,7 @@ package org.apache.commons.runtime.excep
* @since Runtime 1.0
*/
-public class NoSuchObjectException extends RuntimeException
+public class NoSuchObjectException extends SystemException
{
public NoSuchObjectException()
Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/exception/UnsupportedOperatingSystemException.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/exception/UnsupportedOperatingSystemException.java?rev=1091867&r1=1091866&r2=1091867&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/exception/UnsupportedOperatingSystemException.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/exception/UnsupportedOperatingSystemException.java Wed Apr 13 18:20:01 2011
@@ -22,7 +22,7 @@ package org.apache.commons.runtime.excep
*
*/
-public class UnsupportedOperatingSystemException extends RuntimeException
+public class UnsupportedOperatingSystemException extends SystemException
{
public UnsupportedOperatingSystemException()
Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/linux/SELinux.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/linux/SELinux.java?rev=1091867&r1=1091866&r2=1091867&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/linux/SELinux.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/linux/SELinux.java Wed Apr 13 18:20:01 2011
@@ -16,7 +16,7 @@
package org.apache.commons.runtime.platform.linux;
-import org.apache.commons.runtime.exception.OperatingSystemException;
+import org.apache.commons.runtime.exception.SystemException;
import org.apache.commons.runtime.exception.UnsupportedOperatingSystemException;
/**
@@ -36,7 +36,7 @@ public class SELinux
* @param type OS type to test.
*/
private static native boolean enabled0()
- throws OperatingSystemException;
+ throws SystemException;
/**
* Check if we are running on {@code SELinux} kernel.
@@ -47,7 +47,7 @@ public class SELinux
* operating system.
*/
public static boolean isEnabled()
- throws OperatingSystemException, UnsupportedOperatingSystemException
+ throws SystemException, UnsupportedOperatingSystemException
{
try {
return enabled0();
Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/RegistryKey.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/RegistryKey.java?rev=1091867&r1=1091866&r2=1091867&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/RegistryKey.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/RegistryKey.java Wed Apr 13 18:20:01 2011
@@ -21,7 +21,7 @@ import java.io.Flushable;
import java.io.IOException;
import java.io.SyncFailedException;
import java.util.EnumSet;
-import org.apache.commons.runtime.io.Status;
+import org.apache.commons.runtime.Status;
/**
* Registry key class.
Modified: commons/sandbox/runtime/trunk/src/main/native/shared/error.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/error.c?rev=1091867&r1=1091866&r2=1091867&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/error.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/error.c Wed Apr 13 18:20:01 2011
@@ -748,7 +748,7 @@ AcrReleaseExceptionClasses(JNI_STDENV)
}
}
-ACR_IO_EXPORT(jstring, Status, describe)(JNI_STDARGS, jint err)
+ACR_JNI_EXPORT(jstring, Status, describe)(JNI_STDARGS, jint err)
{
char buf[ACR_MBUFF_SIZ] = "";
@@ -756,7 +756,7 @@ ACR_IO_EXPORT(jstring, Status, describe)
return AcrNewJavaStringA(env, buf);
}
-ACR_IO_EXPORT(jboolean, Status, is0)(JNI_STDARGS, jint what, jint err)
+ACR_JNI_EXPORT(jboolean, Status, is0)(JNI_STDARGS, jint what, jint err)
{
jboolean rv = JNI_FALSE;
switch (what) {
@@ -769,6 +769,9 @@ ACR_IO_EXPORT(jboolean, Status, is0)(JNI
case 2:
if (ACR_STATUS_IS_ENOENT(err)) rv = JNI_TRUE;
break;
+ case 3:
+ if (ACR_STATUS_IS_EBUSY(err)) rv = JNI_TRUE;
+ break;
default:
break;
}