ace-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From j...@apache.org
Subject svn commit: r1521522 - in /ace/trunk/org.apache.ace.agent: src/org/apache/ace/agent/impl/ test/org/apache/ace/agent/testutil/
Date Tue, 10 Sep 2013 15:15:45 GMT
Author: jawi
Date: Tue Sep 10 15:15:45 2013
New Revision: 1521522

URL: http://svn.apache.org/r1521522
Log:
Forgot to add newly added files.

Added:
    ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/InternalConstants.java  
(with props)
    ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/ReflectionUtil.java   (with
props)
    ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/ResettableTimer.java   (with
props)
    ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/testutil/SynchronousExecutorService.java
  (with props)

Added: ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/InternalConstants.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/InternalConstants.java?rev=1521522&view=auto
==============================================================================
--- ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/InternalConstants.java (added)
+++ ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/InternalConstants.java Tue
Sep 10 15:15:45 2013
@@ -0,0 +1,31 @@
+/*
+ * 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.ace.agent.impl;
+
+/**
+ * Represents internal constants.
+ */
+public interface InternalConstants {
+    /**
+     * Event topic used to report changes in the agent's configuration.
+     */
+    String AGENT_CONFIG_CHANGED = "agent/config/CHANGED";
+
+}

Propchange: ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/InternalConstants.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/ReflectionUtil.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/ReflectionUtil.java?rev=1521522&view=auto
==============================================================================
--- ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/ReflectionUtil.java (added)
+++ ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/ReflectionUtil.java Tue Sep
10 15:15:45 2013
@@ -0,0 +1,66 @@
+/*
+ * 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.ace.agent.impl;
+
+import java.lang.reflect.AccessibleObject;
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+
+/**
+ * Provides some utility methods for use with reflection.
+ */
+final class ReflectionUtil {
+
+    static void configureField(Object object, Class<?> iface, Object instance) {
+        // Note: Does not check super classes!
+        Class<?> clazz = object.getClass();
+        do {
+            Field[] fields = clazz.getDeclaredFields();
+            AccessibleObject.setAccessible(fields, true);
+            for (int j = 0; j < fields.length; j++) {
+                if (iface.equals(fields[j].getType())) {
+                    try {
+                        synchronized (new Object()) {
+                            fields[j].set(object, instance);
+                        }
+                    }
+                    catch (Exception e) {
+                        throw new IllegalStateException("Could not set field " + fields[j].getName()
+ " on " + object, e);
+                    }
+                }
+            }
+            clazz = clazz.getSuperclass();
+        }
+        while (!Object.class.equals(clazz));
+    }
+
+    static Object invokeMethod(Object object, String methodName, Class<?>[] signature,
Object[] parameters) {
+        // Note: Does not check super classes!
+        Class<?> clazz = object.getClass();
+        try {
+            Method method = clazz.getDeclaredMethod(methodName, signature);
+            return method.invoke(object, parameters);
+        }
+        catch (Exception e) {
+            e.printStackTrace();
+        }
+        return null;
+    }
+}

Propchange: ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/ReflectionUtil.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/ResettableTimer.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/ResettableTimer.java?rev=1521522&view=auto
==============================================================================
--- ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/ResettableTimer.java (added)
+++ ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/ResettableTimer.java Tue
Sep 10 15:15:45 2013
@@ -0,0 +1,133 @@
+/*
+ * 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.ace.agent.impl;
+
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.ScheduledFuture;
+import java.util.concurrent.ScheduledThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicReference;
+
+/**
+ * Provides a timer that can be reset.
+ * <p>
+ * Taken from Apache Felix UserAdmin File-based store implementation.
+ * </p>
+ */
+final class ResettableTimer {
+    private final ScheduledExecutorService m_executor;
+    private final Runnable m_task;
+    private final long m_timeout;
+    private final TimeUnit m_timeUnit;
+    private final AtomicReference<ScheduledFuture<?>> m_futureRef;
+
+    /**
+     * Creates a new {@link ResettableTimer} calling a given task when a given timeout exceeds.
+     * 
+     * @param task
+     *            the task to execute upon timout, cannot be <code>null</code>;
+     * @param timeout
+     *            the timeout value, > 0;
+     * @param unit
+     *            the time unit of the timeout value, cannot be <code>null</code>.
+     */
+    public ResettableTimer(Runnable task, long timeout, TimeUnit unit) {
+        this(new ScheduledThreadPoolExecutor(1), task, timeout, unit);
+    }
+
+    /**
+     * Creates a new {@link ResettableTimer} calling a given task when a given timeout exceeds.
+     * 
+     * @param executor
+     *            the executor to use to execute the task, cannot be <code>null</code>;
+     * @param task
+     *            the task to execute upon timout, cannot be <code>null</code>;
+     * @param timeout
+     *            the timeout value, > 0;
+     * @param unit
+     *            the time unit of the timeout value, cannot be <code>null</code>.
+     */
+    public ResettableTimer(ScheduledExecutorService executor, Runnable task, long timeout,
TimeUnit unit) {
+        if (executor == null) {
+            throw new IllegalArgumentException("Executor cannot be null!");
+        }
+        if (task == null) {
+            throw new IllegalArgumentException("Task cannot be null!");
+        }
+        if (timeout <= 0) {
+            throw new IllegalArgumentException("Timeout cannot be negative!");
+        }
+        if (unit == null) {
+            throw new IllegalArgumentException("TimeUnit cannot be null!");
+        }
+
+        m_executor = executor;
+        m_task = task;
+        m_timeout = timeout;
+        m_timeUnit = unit;
+
+        m_futureRef = new AtomicReference<ScheduledFuture<?>>();
+    }
+
+    /**
+     * Returns the state of this timer.
+     * 
+     * @return <code>true</code> if this timer is shut down, <code>false</code>
otherwise.
+     */
+    public boolean isShutDown() {
+        return m_executor.isShutdown();
+    }
+
+    /**
+     * Schedules the task for execution with the contained timeout. If a task is already
pending or running, it will be
+     * cancelled (not interrupted). The new task will be scheduled to run in now + timeout.
+     */
+    public ScheduledFuture<?> schedule() {
+        ScheduledFuture<?> currentTask = cancelCurrentTask();
+        ScheduledFuture<?> newTask = m_executor.schedule(m_task, m_timeout, m_timeUnit);
+        m_futureRef.compareAndSet(currentTask, newTask);
+        return newTask;
+    }
+
+    /**
+     * Shuts down this timer, allowing any pending tasks to execute. After this method is
called, {@link #schedule()}
+     * may no longer be called.
+     */
+    public void shutDown() {
+        m_executor.shutdown();
+        try {
+            m_executor.awaitTermination(2 * m_timeout, m_timeUnit);
+        }
+        catch (InterruptedException e) {
+            Thread.currentThread().interrupt();
+        }
+    }
+
+    /**
+     * @return the current task, or <code>null</code> if no task is available.
+     */
+    private ScheduledFuture<?> cancelCurrentTask() {
+        ScheduledFuture<?> currentTask = m_futureRef.get();
+        if (currentTask != null) {
+            // Doesn't matter for completed tasks...
+            currentTask.cancel(false /* mayInterruptIfRunning */);
+        }
+        return currentTask;
+    }
+}

Propchange: ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/ResettableTimer.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/testutil/SynchronousExecutorService.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/testutil/SynchronousExecutorService.java?rev=1521522&view=auto
==============================================================================
--- ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/testutil/SynchronousExecutorService.java
(added)
+++ ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/testutil/SynchronousExecutorService.java
Tue Sep 10 15:15:45 2013
@@ -0,0 +1,125 @@
+/*
+ * 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.ace.agent.testutil;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.List;
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Future;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.ScheduledFuture;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+
+/**
+ * Provides a fake implementation of {@link ScheduledExecutorService} that does all work
synchronously.
+ */
+public class SynchronousExecutorService implements ScheduledExecutorService {
+    @Override
+    public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException
{
+        return false;
+    }
+
+    @Override
+    public void execute(Runnable command) {
+        command.run();
+    }
+
+    @Override
+    public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>>
tasks) throws InterruptedException {
+        throw new UnsupportedOperationException();
+    }
+
+    @Override
+    public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>>
tasks, long timeout, TimeUnit unit) throws InterruptedException {
+        throw new UnsupportedOperationException();
+    }
+
+    @Override
+    public <T> T invokeAny(Collection<? extends Callable<T>> tasks) throws
InterruptedException, ExecutionException {
+        throw new UnsupportedOperationException();
+    }
+
+    @Override
+    public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long
timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException
{
+        throw new UnsupportedOperationException();
+    }
+
+    @Override
+    public boolean isShutdown() {
+        return false;
+    }
+
+    @Override
+    public boolean isTerminated() {
+        return false;
+    }
+
+    @Override
+    public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay,
TimeUnit unit) {
+        throw new UnsupportedOperationException();
+    }
+
+    @Override
+    public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit)
{
+        execute(command);
+        return null;
+    }
+
+    @Override
+    public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay,
long period, TimeUnit unit) {
+        execute(command);
+        return null;
+    }
+
+    @Override
+    public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay,
long delay, TimeUnit unit) {
+        execute(command);
+        return null;
+    }
+
+    @Override
+    public void shutdown() {
+        // Nop
+    }
+
+    @Override
+    public List<Runnable> shutdownNow() {
+        return Arrays.asList();
+    }
+
+    @Override
+    public <T> Future<T> submit(Callable<T> task) {
+        throw new UnsupportedOperationException();
+    }
+
+    @Override
+    public Future<?> submit(Runnable task) {
+        execute(task);
+        return null;
+    }
+
+    @Override
+    public <T> Future<T> submit(Runnable task, T result) {
+        execute(task);
+        return null;
+    }
+}

Propchange: ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/testutil/SynchronousExecutorService.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message