From commits-return-3573-apmail-ace-commits-archive=ace.apache.org@ace.apache.org Tue Sep 10 15:16:08 2013 Return-Path: X-Original-To: apmail-ace-commits-archive@www.apache.org Delivered-To: apmail-ace-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 6B3AEC4F6 for ; Tue, 10 Sep 2013 15:16:08 +0000 (UTC) Received: (qmail 64668 invoked by uid 500); 10 Sep 2013 15:16:08 -0000 Delivered-To: apmail-ace-commits-archive@ace.apache.org Received: (qmail 64649 invoked by uid 500); 10 Sep 2013 15:16:08 -0000 Mailing-List: contact commits-help@ace.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@ace.apache.org Delivered-To: mailing list commits@ace.apache.org Received: (qmail 64634 invoked by uid 99); 10 Sep 2013 15:16:07 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 10 Sep 2013 15:16:07 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 10 Sep 2013 15:16:06 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id ED3FA2388860; Tue, 10 Sep 2013 15:15:45 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit 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 -0000 To: commits@ace.apache.org From: jawi@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20130910151545.ED3FA2388860@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org 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. + *

+ * Taken from Apache Felix UserAdmin File-based store implementation. + *

+ */ +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> 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 null; + * @param timeout + * the timeout value, > 0; + * @param unit + * the time unit of the timeout value, cannot be null. + */ + 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 null; + * @param task + * the task to execute upon timout, cannot be null; + * @param timeout + * the timeout value, > 0; + * @param unit + * the time unit of the timeout value, cannot be null. + */ + 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>(); + } + + /** + * Returns the state of this timer. + * + * @return true if this timer is shut down, false 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 null 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 List> invokeAll(Collection> tasks) throws InterruptedException { + throw new UnsupportedOperationException(); + } + + @Override + public List> invokeAll(Collection> tasks, long timeout, TimeUnit unit) throws InterruptedException { + throw new UnsupportedOperationException(); + } + + @Override + public T invokeAny(Collection> tasks) throws InterruptedException, ExecutionException { + throw new UnsupportedOperationException(); + } + + @Override + public T invokeAny(Collection> 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 ScheduledFuture schedule(Callable 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 shutdownNow() { + return Arrays.asList(); + } + + @Override + public Future submit(Callable task) { + throw new UnsupportedOperationException(); + } + + @Override + public Future submit(Runnable task) { + execute(task); + return null; + } + + @Override + public Future 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