The Listener was simply an enabler for the publishing of events when
commands are executed (the motivation for that one is mostly
auditing).
I found the listener could be reused want to audit it in a different
way, so i made it public in case, but I don't have any real use case
for now beyond the EventAdmin listener.

On Wed, Jan 5, 2011 at 16:07, Richard S. Hall <[email protected]> wrote:
> On 1/5/11 3:30, Guillaume Nodet wrote:
>>
>> I've moved the new interface in a new package in 1055339
>
> Thanks.
>
> Would it also be possible to get more motivation and background for these
> features in the JIRA issues? Kind of difficult to know if they are
> worthwhile or not.
>
> -> richard
>>
>> On Wed, Jan 5, 2011 at 02:49, Richard S. Hall<[email protected]>
>>  wrote:
>>>
>>> If you are not planning to push these ideas into the RFC, then you should
>>> be
>>> creating an implementation-specific package into which to place them,
>>> since
>>> the o.a.f.s.c is intended to become the eventual org.osgi.service.command
>>> package.
>>>
>>> ->  richard
>>>
>>> On 1/4/11 8:18 PM, [email protected] wrote:
>>>>
>>>> Author: gnodet
>>>> Date: Wed Jan  5 01:18:51 2011
>>>> New Revision: 1055258
>>>>
>>>> URL: http://svn.apache.org/viewvc?rev=1055258&view=rev
>>>> Log:
>>>> [FELIX-2761][FELIX-2764] Add a listener interface called when executing
>>>> commands, send an osgi event if EventAdmin is present
>>>>
>>>> Added:
>>>>
>>>>
>>>> felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/activator/EventAdminListener.java
>>>>
>>>>
>>>> felix/trunk/gogo/runtime/src/main/java/org/apache/felix/service/command/CommandSessionListener.java
>>>> Modified:
>>>>     felix/trunk/gogo/runtime/pom.xml
>>>>
>>>>
>>>> felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/CommandProcessorImpl.java
>>>>
>>>>
>>>> felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/CommandSessionImpl.java
>>>>
>>>>
>>>> felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/activator/Activator.java
>>>>
>>>> Modified: felix/trunk/gogo/runtime/pom.xml
>>>> URL:
>>>>
>>>> http://svn.apache.org/viewvc/felix/trunk/gogo/runtime/pom.xml?rev=1055258&r1=1055257&r2=1055258&view=diff
>>>>
>>>>
>>>> ==============================================================================
>>>> --- felix/trunk/gogo/runtime/pom.xml (original)
>>>> +++ felix/trunk/gogo/runtime/pom.xml Wed Jan  5 01:18:51 2011
>>>> @@ -60,6 +60,7 @@
>>>>                              org.apache.felix.service.threadio;
>>>> version=${project.version}; status="provisional"; mandatory:="status"
>>>>                          </Export-Package>
>>>>                          <Import-Package>
>>>> +                            org.osgi.service.event*;
>>>> resolution:=optional,
>>>>                              org.osgi.service.log*;
>>>> resolution:=optional,
>>>>                              org.osgi.service.packageadmin*;
>>>> resolution:=optional,
>>>>                              org.osgi.service.startlevel*;
>>>> resolution:=optional,
>>>>
>>>> Modified:
>>>>
>>>> felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/CommandProcessorImpl.java
>>>> URL:
>>>>
>>>> http://svn.apache.org/viewvc/felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/CommandProcessorImpl.java?rev=1055258&r1=1055257&r2=1055258&view=diff
>>>>
>>>>
>>>> ==============================================================================
>>>> ---
>>>>
>>>> felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/CommandProcessorImpl.java
>>>> (original)
>>>> +++
>>>>
>>>> felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/CommandProcessorImpl.java
>>>> Wed Jan  5 01:18:51 2011
>>>> @@ -21,10 +21,18 @@ package org.apache.felix.gogo.runtime;
>>>>  import java.io.InputStream;
>>>>  import java.io.PrintStream;
>>>>  import java.lang.reflect.Method;
>>>> -import java.util.*;
>>>> +import java.util.HashMap;
>>>> +import java.util.HashSet;
>>>> +import java.util.Iterator;
>>>> +import java.util.LinkedHashMap;
>>>> +import java.util.Map;
>>>>  import java.util.Map.Entry;
>>>> +import java.util.Set;
>>>> +import java.util.TreeSet;
>>>> +import java.util.WeakHashMap;
>>>> +import java.util.concurrent.CopyOnWriteArraySet;
>>>>
>>>> -import org.osgi.framework.BundleContext;
>>>> +import org.apache.felix.service.command.CommandSessionListener;
>>>>  import org.apache.felix.service.command.CommandProcessor;
>>>>  import org.apache.felix.service.command.CommandSession;
>>>>  import org.apache.felix.service.command.Converter;
>>>> @@ -34,6 +42,7 @@ import org.apache.felix.service.threadio
>>>>  public class CommandProcessorImpl implements CommandProcessor
>>>>  {
>>>>      protected final Set<Converter>    converters = new
>>>> HashSet<Converter>();
>>>> +    protected final Set<CommandSessionListener>    listeners = new
>>>> CopyOnWriteArraySet<CommandSessionListener>();
>>>>      protected final Map<String, Object>    commands = new
>>>> LinkedHashMap<String, Object>();
>>>>      protected final Map<String, Object>    constants = new
>>>> HashMap<String,
>>>> Object>();
>>>>      protected final ThreadIO threadIO;
>>>> @@ -69,6 +78,17 @@ public class CommandProcessorImpl implem
>>>>          converters.remove(c);
>>>>      }
>>>>
>>>> +    public void addListener(CommandSessionListener l)
>>>> +    {
>>>> +        listeners.add(l);
>>>> +    }
>>>> +
>>>> +    public void removeListener(CommandSessionListener l)
>>>> +    {
>>>> +        listeners.remove(l);
>>>> +    }
>>>> +
>>>> +
>>>>      public Set<String>    getCommands()
>>>>      {
>>>>          return commands.keySet();
>>>> @@ -243,4 +263,50 @@ public class CommandProcessorImpl implem
>>>>
>>>>          return session.execute(buf);
>>>>      }
>>>> +
>>>> +    void beforeExecute(CommandSession session, CharSequence
>>>> commandline)
>>>> +    {
>>>> +        for (CommandSessionListener l : listeners)
>>>> +        {
>>>> +            try
>>>> +            {
>>>> +                l.beforeExecute(session, commandline);
>>>> +            }
>>>> +            catch (Throwable t)
>>>> +            {
>>>> +                // Ignore
>>>> +            }
>>>> +        }
>>>> +    }
>>>> +
>>>> +    void afterExecute(CommandSession session, CharSequence commandline,
>>>> Exception exception)
>>>> +    {
>>>> +        for (CommandSessionListener l : listeners)
>>>> +        {
>>>> +            try
>>>> +            {
>>>> +                l.afterExecute(session, commandline, exception);
>>>> +            }
>>>> +            catch (Throwable t)
>>>> +            {
>>>> +                // Ignore
>>>> +            }
>>>> +        }
>>>> +    }
>>>> +
>>>> +    void afterExecute(CommandSession session, CharSequence commandline,
>>>> Object result)
>>>> +    {
>>>> +        for (CommandSessionListener l : listeners)
>>>> +        {
>>>> +            try
>>>> +            {
>>>> +                l.afterExecute(session, commandline, result);
>>>> +            }
>>>> +            catch (Throwable t)
>>>> +            {
>>>> +                // Ignore
>>>> +            }
>>>> +        }
>>>> +    }
>>>> +
>>>>  }
>>>>
>>>> Modified:
>>>>
>>>> felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/CommandSessionImpl.java
>>>> URL:
>>>>
>>>> http://svn.apache.org/viewvc/felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/CommandSessionImpl.java?rev=1055258&r1=1055257&r2=1055258&view=diff
>>>>
>>>>
>>>> ==============================================================================
>>>> ---
>>>>
>>>> felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/CommandSessionImpl.java
>>>> (original)
>>>> +++
>>>>
>>>> felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/CommandSessionImpl.java
>>>> Wed Jan  5 01:18:51 2011
>>>> @@ -81,18 +81,18 @@ public class CommandSessionImpl implemen
>>>>              throw new IllegalStateException(SESSION_CLOSED);
>>>>          }
>>>>
>>>> -        beforeExecute(commandline);
>>>> +        processor.beforeExecute(this, commandline);
>>>>
>>>>          try
>>>>          {
>>>>              Closure impl = new Closure(this, null, commandline);
>>>>              Object result = impl.execute(this, null);
>>>> -            afterExecute(commandline, result);
>>>> +            processor.afterExecute(this, commandline, result);
>>>>              return result;
>>>>          }
>>>>          catch (Exception e)
>>>>          {
>>>> -            afterExecute(commandline, e);
>>>> +            processor.afterExecute(this, commandline, e);
>>>>              throw e;
>>>>          }
>>>>      }
>>>> @@ -386,19 +386,4 @@ public class CommandSessionImpl implemen
>>>>          }
>>>>      }
>>>>
>>>> -    protected void beforeExecute(CharSequence commandline)
>>>> -    {
>>>> -        // Centralized callback for derived implementation
>>>> -    }
>>>> -
>>>> -    protected void afterExecute(CharSequence commandline, Exception
>>>> exception)
>>>> -    {
>>>> -        // Centralized callback for derived implementation
>>>> -    }
>>>> -
>>>> -    protected void afterExecute(CharSequence commandline, Object
>>>> result)
>>>> -    {
>>>> -        // Centralized callback for derived implementation
>>>> -    }
>>>> -
>>>>  }
>>>>
>>>> Modified:
>>>>
>>>> felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/activator/Activator.java
>>>> URL:
>>>>
>>>> http://svn.apache.org/viewvc/felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/activator/Activator.java?rev=1055258&r1=1055257&r2=1055258&view=diff
>>>>
>>>>
>>>> ==============================================================================
>>>> ---
>>>>
>>>> felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/activator/Activator.java
>>>> (original)
>>>> +++
>>>>
>>>> felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/activator/Activator.java
>>>> Wed Jan  5 01:18:51 2011
>>>> @@ -23,6 +23,7 @@ import java.util.List;
>>>>
>>>>  import org.apache.felix.gogo.runtime.CommandProcessorImpl;
>>>>  import org.apache.felix.gogo.runtime.CommandProxy;
>>>> +import org.apache.felix.service.command.CommandSessionListener;
>>>>  import org.apache.felix.gogo.runtime.threadio.ThreadIOImpl;
>>>>  import org.osgi.framework.BundleActivator;
>>>>  import org.osgi.framework.BundleContext;
>>>> @@ -42,6 +43,7 @@ public class Activator implements Bundle
>>>>      private ThreadIOImpl threadio;
>>>>      private ServiceTracker commandTracker;
>>>>      private ServiceTracker converterTracker;
>>>> +    private ServiceTracker listenerTracker;
>>>>      private ServiceRegistration processorRegistration;
>>>>      private ServiceRegistration threadioRegistration;
>>>>
>>>> @@ -50,6 +52,14 @@ public class Activator implements Bundle
>>>>      protected ServiceRegistration newProcessor(ThreadIO tio,
>>>> BundleContext context)
>>>>      {
>>>>          processor = new CommandProcessorImpl(tio);
>>>> +        try
>>>> +        {
>>>> +            processor.addListener(new EventAdminListener(context));
>>>> +        }
>>>> +        catch (NoClassDefFoundError error)
>>>> +        {
>>>> +            // Ignore the listener if EventAdmin package isn't present
>>>> +        }
>>>>
>>>>          // Setup the variables and commands exposed in an OSGi
>>>> environment.
>>>>          processor.addConstant(CONTEXT, context);
>>>> @@ -90,6 +100,23 @@ public class Activator implements Bundle
>>>>              }
>>>>          };
>>>>          converterTracker.open();
>>>> +
>>>> +        listenerTracker = new ServiceTracker(context,
>>>> CommandSessionListener.class.getName(), null)
>>>> +        {
>>>> +           �...@override
>>>> +            public Object addingService(ServiceReference reference) {
>>>> +                CommandSessionListener listener =
>>>> (CommandSessionListener) super.addingService(reference);
>>>> +                processor.addListener(listener);
>>>> +                return listener;
>>>> +            }
>>>> +
>>>> +           �...@override
>>>> +            public void removedService(ServiceReference reference,
>>>> Object
>>>> service) {
>>>> +                processor.removeListener((CommandSessionListener)
>>>> service);
>>>> +                super.removedService(reference, service);
>>>> +            }
>>>> +        };
>>>> +        listenerTracker.open();
>>>>      }
>>>>
>>>>      public void stop(BundleContext context) throws Exception
>>>> @@ -98,6 +125,7 @@ public class Activator implements Bundle
>>>>          threadioRegistration.unregister();
>>>>          commandTracker.close();
>>>>          converterTracker.close();
>>>> +        listenerTracker.close();
>>>>          threadio.stop();
>>>>          processor.stop();
>>>>      }
>>>>
>>>> Added:
>>>>
>>>> felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/activator/EventAdminListener.java
>>>> URL:
>>>>
>>>> http://svn.apache.org/viewvc/felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/activator/EventAdminListener.java?rev=1055258&view=auto
>>>>
>>>>
>>>> ==============================================================================
>>>> ---
>>>>
>>>> felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/activator/EventAdminListener.java
>>>> (added)
>>>> +++
>>>>
>>>> felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/activator/EventAdminListener.java
>>>> Wed Jan  5 01:18:51 2011
>>>> @@ -0,0 +1,59 @@
>>>> +/*
>>>> + * 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.felix.gogo.runtime.activator;
>>>> +
>>>> +import java.util.Properties;
>>>> +
>>>> +import org.apache.felix.service.command.CommandSession;
>>>> +import org.apache.felix.service.command.CommandSessionListener;
>>>> +import org.osgi.framework.BundleContext;
>>>> +import org.osgi.service.event.Event;
>>>> +import org.osgi.service.event.EventAdmin;
>>>> +import org.osgi.util.tracker.ServiceTracker;
>>>> +
>>>> +public class EventAdminListener implements CommandSessionListener
>>>> +{
>>>> +
>>>> +    private BundleContext bundleContext;
>>>> +    private ServiceTracker tracker;
>>>> +
>>>> +    public EventAdminListener(BundleContext bundleContext)
>>>> +    {
>>>> +        this.bundleContext = bundleContext;
>>>> +        tracker = new ServiceTracker(bundleContext,
>>>> EventAdmin.class.getName(), null);
>>>> +        tracker.open();
>>>> +    }
>>>> +
>>>> +    public void beforeExecute(CommandSession session, CharSequence
>>>> command) {
>>>> +        EventAdmin admin = (EventAdmin) tracker.getService();
>>>> +        if (admin != null) {
>>>> +            Properties props = new Properties();
>>>> +            props.setProperty("command", command.toString());
>>>> +            Event event = new
>>>> Event("org/apache/felix/service/command/EXECUTING", props);
>>>> +            admin.postEvent(event);
>>>> +        }
>>>> +    }
>>>> +
>>>> +    public void afterExecute(CommandSession session, CharSequence
>>>> command, Exception exception) {
>>>> +    }
>>>> +
>>>> +    public void afterExecute(CommandSession session, CharSequence
>>>> command, Object result) {
>>>> +    }
>>>> +
>>>> +}
>>>>
>>>> Added:
>>>>
>>>> felix/trunk/gogo/runtime/src/main/java/org/apache/felix/service/command/CommandSessionListener.java
>>>> URL:
>>>>
>>>> http://svn.apache.org/viewvc/felix/trunk/gogo/runtime/src/main/java/org/apache/felix/service/command/CommandSessionListener.java?rev=1055258&view=auto
>>>>
>>>>
>>>> ==============================================================================
>>>> ---
>>>>
>>>> felix/trunk/gogo/runtime/src/main/java/org/apache/felix/service/command/CommandSessionListener.java
>>>> (added)
>>>> +++
>>>>
>>>> felix/trunk/gogo/runtime/src/main/java/org/apache/felix/service/command/CommandSessionListener.java
>>>> Wed Jan  5 01:18:51 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.
>>>> + */
>>>> +package org.apache.felix.service.command;
>>>> +
>>>> +/**
>>>> + * Listener for command executions.
>>>> + *
>>>> + * Such listeners must be registered in the OSGi registry and will be
>>>> called
>>>> + * by the CommandProcessor when a command line is executed in a given
>>>> session.
>>>> + */
>>>> +public interface CommandSessionListener {
>>>> +
>>>> +    void beforeExecute(CommandSession session, CharSequence command);
>>>> +
>>>> +    void afterExecute(CommandSession session, CharSequence command,
>>>> Exception exception);
>>>> +
>>>> +    void afterExecute(CommandSession session, CharSequence command,
>>>> Object result);
>>>> +
>>>> +}
>>>>
>>>>
>>
>>
>



-- 
Cheers,
Guillaume Nodet
------------------------
Blog: http://gnodet.blogspot.com/
------------------------
Open Source SOA
http://fusesource.com

Reply via email to