commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mt...@apache.org
Subject svn commit: r1099338 - in /commons/sandbox/runtime/trunk/src/main: java/org/apache/commons/runtime/io/Descriptor.java native/Makefile.msc.in native/Makefile.unx.in native/include/acr/descriptor.h native/include/acr/jnimacros.h native/shared/descriptor.c
Date Wed, 04 May 2011 06:56:35 GMT
Author: mturk
Date: Wed May  4 06:56:35 2011
New Revision: 1099338

URL: http://svn.apache.org/viewvc?rev=1099338&view=rev
Log:
Add Descriptor class

Added:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Descriptor.java
  (with props)
    commons/sandbox/runtime/trunk/src/main/native/include/acr/descriptor.h   (with props)
    commons/sandbox/runtime/trunk/src/main/native/shared/descriptor.c   (with props)
Modified:
    commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in
    commons/sandbox/runtime/trunk/src/main/native/Makefile.unx.in
    commons/sandbox/runtime/trunk/src/main/native/include/acr/jnimacros.h

Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Descriptor.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Descriptor.java?rev=1099338&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Descriptor.java
(added)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Descriptor.java
Wed May  4 06:56:35 2011
@@ -0,0 +1,178 @@
+/* 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 java.io.Closeable;
+import java.io.Flushable;
+import java.io.IOException;
+import java.io.SyncFailedException;
+
+/** Represents the Operating System object descriptor.
+ * <p>
+ * Object descriptors represent the allocated system resource that
+ * can be of any type supported. Such objects are {@code files} ,
+ * {@code sockets} , {@code processes} etc...
+ * </p>
+ * <p>
+ * Descriptor is Operating system dependent usually encapsulating
+ * the {@code int} on unixes and {@code HANDLE} on Microsoft Windows.
+ * </p>
+ * @since Runtime 1.0
+ */
+public abstract class Descriptor implements Closeable, Syncable
+{
+
+    /** Operating system descriptor.
+     */
+    protected int fd;
+
+    /**
+     * Creates a new object.
+     */
+    protected  Descriptor()
+    {
+        fd = -1;
+    }
+
+    private static native void init0();
+    static {
+        // Initialize Descriptor class.
+        // Used to find the fd field inside native code.
+        init0();
+    }
+
+    /**
+     * Free the allocated resource by the Operating system.
+     * <p>
+     * Note that {@code Object.finalize()} method will call
+     * this function. However if the native code can block for
+     * long time explicit {@code close()} should be called.
+     * </p>
+     * @see java.io.Closeable#close()
+     * @throws IOException if an I/O error occurs.
+     */
+    public abstract void close()
+        throws IOException;
+
+    /**
+     * Force the underlying object to synchonize it's state with the storage
+     * device.
+     * <p>
+     * Sync does not necessarily syncronize it's meta data unless that metadata
+     * is needed in order to allow a subsequent data retrieval to be
+     * correctly handled. For example, changes to time of last  access and
+     * time of last modification do not not require flushing
+     * because they are not necessary for a subsequent data read to be
+     * handled correctly.  On the other hand, a change to the file size
+     * would require a metadata flush.
+     * </p>
+     * <p>
+     * The aim of {@code sync} is to reduce disk activity for applications
+     * that do not require all metadata to be synchronised with the disk.
+     * </p>
+     *
+     * @throws SyncFailedException when the object cannot be synced.
+     * @throws IOException if an I/O error occurs.
+     */
+    public abstract void sync()
+        throws SyncFailedException, IOException;
+
+    /**
+     * Flushed the underlying object 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
+     * (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
+     * {@code this} Descriptor.
+     * </p>
+     *
+     * @throws SyncFailedException when the object cannot be flushed.
+     * @throws IOException if an I/O error occurs.
+     */
+    public abstract void flush()
+        throws SyncFailedException, IOException;
+
+    /**
+     * Check if the underlying Operating system descriptor is valid.
+     * @return {@code true} if descriptor is valid and not closed
+     *         {@code false} otherwise.
+     */
+    public boolean valid()
+    {
+        // true if both int is negative or zero
+        // Descriptor is always assured to be above the stderr (#3)
+        if (fd == -1 || fd == 0)
+            return false;
+        else
+            return true;
+    }
+
+    /**
+     * Get underlying Operating system descriptor.
+     * @return operating system descriptor.
+     */
+    public int fd()
+    {
+        return fd;
+    }
+
+    /**
+     * Compares this {@code Descriptor} to the specified object.
+     *
+     * @param other a {@code Descriptor}
+     * @return  true if the class of this {@code Descriptor} object and the
+     *      class of {@code other} are exactly equal, and the C/C++
+     *      descriptors being pointed to by these objects are also
+     *      equal. Returns false otherwise.
+     */
+    @Override
+    public boolean equals(Object other)
+    {
+        if (other == null)
+            return false;
+        if (other == this)
+            return true;
+        if (Descriptor.class != other.getClass())
+            return false;
+        if (fd == ((Descriptor)other).fd)
+            return true;
+        else
+            return false;
+    }
+
+    /**
+     * Returns a string representation of the Descriptor.
+     * The returned string is hexadecimal representation of the underlying
+     * descriptor for the {@code pointer} type descriptors and integer for
+     * {@code posix} descriptor types .
+     * @return a string representation of the descriptor.
+     */
+    @Override
+    public String toString()
+    {
+        if (fd >= 0) {
+            return "#" + Integer.toString(fd);
+        }
+        else {
+            return "(nil)";
+        }
+    }
+
+}
+

Propchange: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Descriptor.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in?rev=1099338&r1=1099337&r2=1099338&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in (original)
+++ commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in Wed May  4 06:56:35 2011
@@ -108,6 +108,7 @@ LIBSOURCES=\
 	$(TOPDIR)\shared\clazz.c \
 	$(TOPDIR)\shared\constptr.c \
 	$(TOPDIR)\shared\debug.c \
+	$(TOPDIR)\shared\descriptor.c \
 	$(TOPDIR)\shared\directbb.c \
 	$(TOPDIR)\shared\dsock.c \
 	$(TOPDIR)\shared\dsockimpl.c \

Modified: commons/sandbox/runtime/trunk/src/main/native/Makefile.unx.in
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/Makefile.unx.in?rev=1099338&r1=1099337&r2=1099338&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/Makefile.unx.in (original)
+++ commons/sandbox/runtime/trunk/src/main/native/Makefile.unx.in Wed May  4 06:56:35 2011
@@ -102,6 +102,7 @@ LIBSOURCES=\
 	$(TOPDIR)/shared/clazz.c \
 	$(TOPDIR)/shared/constptr.c \
 	$(TOPDIR)/shared/debug.c \
+	$(TOPDIR)/shared/descriptor.c \
 	$(TOPDIR)/shared/directbb.c \
 	$(TOPDIR)/shared/dsock.c \
 	$(TOPDIR)/shared/dsockimpl.c \

Added: commons/sandbox/runtime/trunk/src/main/native/include/acr/descriptor.h
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr/descriptor.h?rev=1099338&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/acr/descriptor.h (added)
+++ commons/sandbox/runtime/trunk/src/main/native/include/acr/descriptor.h Wed May  4 06:56:35
2011
@@ -0,0 +1,35 @@
+/* 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.
+ */
+
+#ifndef _ACR_DESCRIPTOR_H
+#define _ACR_DESCRIPTOR_H
+
+#include "acr/jniapi.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+int
+AcrGetDescriptorFd(JNI_STDARGS);
+int
+AcrSetDescriptorFd(JNI_STDARGS, int fd);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* _ACR_DESCRIPTOR_H */

Propchange: commons/sandbox/runtime/trunk/src/main/native/include/acr/descriptor.h
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: commons/sandbox/runtime/trunk/src/main/native/include/acr/jnimacros.h
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr/jnimacros.h?rev=1099338&r1=1099337&r2=1099338&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/acr/jnimacros.h (original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/acr/jnimacros.h Wed May  4 06:56:35
2011
@@ -52,6 +52,14 @@
             return (E);                                                     \
     } else (void)(0)
 
+#define V_LOAD_METHOD(I)    \
+    if (_m##I##n.i == 0) {                                                  \
+        _m##I##n.i = (*env)->GetMethodID(env, _clazzn.i, _m##I##n.n,        \
+                                        _m##I##n.s);                        \
+        if (_m##I##n.i == 0)                                                \
+            return;                                                         \
+    } else (void)(0)
+        
 #define J_LOPT_METHOD(I)    \
     if (_m##I##n.i == 0) {                                                  \
         _m##I##n.i = (*env)->GetMethodID(env, _clazzn.i, _m##I##n.n,        \
@@ -84,6 +92,13 @@
             return JNI_FALSE;                                               \
     } else (void)(0)
 
+#define V_LOAD_IFIELD(I)   \
+    if (_f##I##n.i == 0) {                                                  \
+        _f##I##n.i = (*env)->GetFieldID(env, _clazzn.i, _f##I##n.n,         \
+                                       _f##I##n.s);                         \
+        if (_f##I##n.i == 0)                                                \
+            return;                                                         \
+    } else (void)(0)
 
 #define GET_IFIELD_J(I, O)  \
     (*env)->GetLongField(env, (O), _f##I##n.i)

Added: commons/sandbox/runtime/trunk/src/main/native/shared/descriptor.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/descriptor.c?rev=1099338&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/descriptor.c (added)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/descriptor.c Wed May  4 06:56:35
2011
@@ -0,0 +1,90 @@
+/* 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.
+ */
+
+/*
+ *
+ * @author Mladen Turk
+ */
+
+#include "acr/descriptor.h"
+#include "acr/clazz.h"
+#include "acr/memory.h"
+#include "acr/unsafe.h"
+
+J_DECLARE_CLAZZ = {
+    INVALID_FIELD_OFFSET,
+    0,
+    0,
+    0,
+    ACR_IO_CP "Descriptor"
+};
+
+J_DECLARE_F_ID(0000) = {
+    INVALID_FIELD_OFFSET,
+    INVALID_FIELD_OFFSET,
+    0,
+    "fd",
+    "I"
+};
+
+
+ACR_IO_EXPORT(void, Descriptor, init0)(JNI_STDARGS)
+{
+    _clazzn.i = (jclass)(*env)->NewGlobalRef(env, obj);
+    if (_clazzn.i == 0)
+        return;
+    V_LOAD_IFIELD(0000);
+    UNSAFE_IFIELD(0000);
+    _clazzn.u = 1;
+}
+
+int
+AcrGetDescriptorFd(JNI_STDARGS)
+{
+    int fd = -1;
+
+    if (J4FLD_OFF(0000) != INVALID_FIELD_OFFSET) {
+        char *oa = *(char **)obj;
+        if (oa != 0) {
+            char *fa = (oa + J4FLD_PTR(0000));
+            fd = *((int *)fa);
+        }
+    }
+    else if (CLAZZ_LOADED) {
+        fd = GET_IFIELD_I(0000, obj);
+    }
+    return fd;
+}
+
+int
+AcrSetDescriptorFd(JNI_STDARGS, int fd)
+{
+    int rc = ACR_EBADF;
+
+    if (J4FLD_OFF(0000) != INVALID_FIELD_OFFSET) {
+        char *oa = *(char **)obj;
+        if (oa != 0) {
+            char *fa = (oa + J4FLD_PTR(0000));
+            *((int *)fa) = fd;
+            rc = 0;
+        }
+    }
+    else if (CLAZZ_LOADED) {
+        SET_IFIELD_I(0000, obj, fd);
+        rc = 0;
+    }
+    return rc;
+}

Propchange: commons/sandbox/runtime/trunk/src/main/native/shared/descriptor.c
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message