openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jrba...@apache.org
Subject svn commit: r981719 [2/2] - in /openjpa/trunk: openjpa-kernel/src/main/java/org/apache/openjpa/conf/ openjpa-kernel/src/main/java/org/apache/openjpa/instrumentation/ openjpa-kernel/src/main/java/org/apache/openjpa/instrumentation/jmx/ openjpa-kernel/sr...
Date Tue, 03 Aug 2010 01:55:03 GMT
Added: openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/instrumentation/AbstractInstrumentationProvider.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/instrumentation/AbstractInstrumentationProvider.java?rev=981719&view=auto
==============================================================================
--- openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/instrumentation/AbstractInstrumentationProvider.java
(added)
+++ openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/instrumentation/AbstractInstrumentationProvider.java
Tue Aug  3 01:55:02 2010
@@ -0,0 +1,173 @@
+/*
+ * 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.openjpa.lib.instrumentation;
+
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+
+import org.apache.openjpa.lib.conf.Configurable;
+import org.apache.openjpa.lib.conf.Configuration;
+import org.apache.openjpa.lib.conf.PluginListValue;
+
+/**
+ * Specialized instrumentation providers can extend this class to get basic
+ * provider state and capabilities.  It implements Configurable so it can
+ * be used within the configuration framework to participate in automatic
+ * configuration.
+ */
+public abstract class AbstractInstrumentationProvider implements InstrumentationProvider,
Configurable {
+
+    private Map<String, Instrument> _instruments = new ConcurrentHashMap<String,
Instrument>();
+    
+    private boolean _started = false;
+    private PluginListValue _instrumentValues;
+    private String _options;
+    private Configuration _config;
+
+    public void setConfiguration(Configuration conf) {
+        _config = conf;
+    }
+    
+    public Configuration getConfiguration() {
+        return _config;
+    }
+    
+    public void startConfiguration() {
+    }
+
+    public void endConfiguration() {
+    }
+    
+    public void setInstrument(String instrument) {
+        _instrumentValues = new PluginListValue("Instrument");
+        if (getInstrumentAliases() != null) {
+            _instrumentValues.setAliases(getInstrumentAliases());
+        }
+        _instrumentValues.setString(instrument);
+        
+        Instrument[] instruments = (Instrument[])_instrumentValues.instantiate(Instrument.class,
_config);
+        for (Instrument inst : instruments) {
+            inst.setProvider(this);
+            _instruments.put(inst.getName(), inst);
+        }
+    }
+    
+    public String getInstrument() {
+        return _instrumentValues.getString();
+    }
+    
+    public void setOptions(String options) {
+        _options = options;
+    }
+    
+    public String getOptions() {
+        return _options;
+    }
+
+    public void addInstrument(Instrument instrument) {
+        if (instrument == null) {
+            return;
+        }
+        instrument.setProvider(this);
+        _instruments.put(instrument.getName(), instrument);
+    }
+    
+    public void initializeInstrument(Instrument instrument, Object context) {
+        initializeInstrument(instrument, _options, context);
+    }
+
+    public void initializeInstrument(Instrument instrument, String options, Object context)
{
+        instrument.setProvider(this);
+        instrument.setOptions(options);
+        instrument.setContext(context);
+        instrument.initialize();
+    }
+
+    public Instrument getInstrumentByName(String name) {
+        return _instruments.get(name);
+    }
+    
+    public Set<Instrument> getInstruments() {
+        return new HashSet<Instrument>(_instruments.values());
+    }
+    
+    public void stopInstruments(InstrumentationLevel level, Object context) {
+        try {
+            Set<Instrument> instruments = getInstruments();
+            for (Instrument instrument : instruments) {
+                if (instrument.getLevel() == level &&
+                    contextEquals(instrument.getContext(),context)) {
+                    stopInstrument(instrument);
+                }
+            }
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    public void startInstruments(InstrumentationLevel level, Object context) {
+        Set<Instrument> instruments = getInstruments();
+        for (Instrument instrument : instruments) {
+            if (instrument.getLevel() == level) {
+                initializeInstrument(instrument, context);
+                startInstrument(instrument);
+            }
+        }
+    }
+    
+    public void stopInstrument(Instrument instrument) {
+        stopInstrument(instrument, true);
+    }
+    
+    public void removeInstrumentByName(String name) {
+        Instrument ins = _instruments.remove(name);
+        if (ins != null) {
+            ins.stop();
+        }
+    }
+    
+    public boolean isStarted() {
+        return _started;
+    }
+    
+    protected void setStarted(boolean started) {
+        _started = started;
+    }
+
+    public String[] getInstrumentAliases() {
+        return null;
+    }
+    
+    public abstract void start();
+
+    public abstract void stop();
+    
+    private static boolean contextEquals(Object ctx1, Object ctx2) {
+        if (ctx1 == ctx2) {
+            return true;
+        }
+        if (ctx1 == null) {
+            return false;
+        }
+        return ctx1.equals(ctx2);
+    }
+    
+}

Propchange: openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/instrumentation/AbstractInstrumentationProvider.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/instrumentation/Instrument.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/instrumentation/Instrument.java?rev=981719&view=auto
==============================================================================
--- openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/instrumentation/Instrument.java
(added)
+++ openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/instrumentation/Instrument.java
Tue Aug  3 01:55:02 2010
@@ -0,0 +1,106 @@
+/*
+ * 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.openjpa.lib.instrumentation;
+
+/**
+ * Interface that must be implemented by instruments.
+ */
+public interface Instrument {
+
+    /**
+     * Returns the name of the instrument.  Must be unique per-provider.
+     */
+    public String getName();
+
+    /**
+     * Returns the options specified for the instrument in string form.
+     * @return options configuration options for the instrument
+     */
+    public String getOptions();
+
+    /**
+     * Sets options to specify for the instrument in standard string form.
+     * ex.  DataCache(Options='Start=true')
+     * @param options options 
+     */
+    public void setOptions(String options);
+
+    /**
+     * Gets the context of the instrument.  Typically, a reference to a broker 
+     * or broker factory.
+     * @return the context associated with the instrument.
+     */
+    public Object getContext();
+    
+    /**
+     * Sets the context of the instrument.  Typically, a reference to a broker 
+     * or broker factory.
+     * @return the context associated with the instrument.
+     */
+    public void setContext(Object context);
+    
+    /**
+     * Sets the instrumentation provider for the instrument.
+     * @param provider instrumentation provider of the instrument
+     */
+    public void setProvider(InstrumentationProvider provider);
+    
+    /**
+     * Gets the instrumentation provider for the instrument.
+     * @return instrumentation provider of the instrument
+     */
+    public InstrumentationProvider getProvider();
+
+    /**
+     * Initializes the instrument.  Depending on the instrument, the provider,
+     * options, and various options may need to be set before calling this method.
+     */
+    public void initialize();
+
+    /**
+     * Gets the instrumentation level of this instrument.  The instrumentation level
+     * determines if and when the instrument will automatically start and stop.
+     * @return  the instrumentation level of the instrument
+     */
+    public InstrumentationLevel getLevel();
+
+    /**
+     * Returns true if the instrument is started.
+     * @return
+     */
+    public boolean isStarted();
+
+    /**
+     * Starts the instrument.  Typically this will be performed through the provider,
+     * but in some cases an instrument will have its own specialized startup.
+     */
+    public void start();
+    
+    /**
+     * Starts the instrument.  Typically this will be performed through the provider,
+     * but in some cases an instrument will have its own specialized shutdown.
+     */
+    public void stop();
+
+    /**
+     * Restarts the instrument.  Typically this will be performed through the provider,
+     * but in some cases an instrument will have its own specialized restart.
+     */
+    public void restart();
+}

Propchange: openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/instrumentation/Instrument.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/instrumentation/InstrumentationLevel.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/instrumentation/InstrumentationLevel.java?rev=981719&view=auto
==============================================================================
--- openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/instrumentation/InstrumentationLevel.java
(added)
+++ openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/instrumentation/InstrumentationLevel.java
Tue Aug  3 01:55:02 2010
@@ -0,0 +1,44 @@
+/*
+ * 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.openjpa.lib.instrumentation;
+
+/**
+ * The instrumentation level can be used to indicate if and when an instrument will be
+ * automatically started and stopped.
+ *
+ */
+public enum InstrumentationLevel {
+    /**
+     *  Start immediately (no special requirements on the broker or factory) and
+     *  stop when the configuration is closed. 
+     */
+    IMMEDIATE, 
+    /**
+     * Start following factory initialization and stop when the factory is closed.
+     */
+    FACTORY,
+    /**
+     * Start following broker/em initialization and stop when the broker/em is closed.
+     */
+    BROKER,
+    /**
+     * Manual start and stop.
+     */
+    MANUAL
+}

Propchange: openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/instrumentation/InstrumentationLevel.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/instrumentation/InstrumentationProvider.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/instrumentation/InstrumentationProvider.java?rev=981719&view=auto
==============================================================================
--- openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/instrumentation/InstrumentationProvider.java
(added)
+++ openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/instrumentation/InstrumentationProvider.java
Tue Aug  3 01:55:02 2010
@@ -0,0 +1,169 @@
+/*
+ * 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.openjpa.lib.instrumentation;
+
+import java.util.Set;
+
+import org.apache.openjpa.lib.conf.Configuration;
+
+/**
+ * Pluggable instrumentation providers (ex. JMX) must implement this interface. It
+ * provides methods for controlling the provider and the instruments instrumented
+ * by the provider.
+ */
+public interface InstrumentationProvider {
+
+    /**
+     * Whether the instrumentation provider started
+     * @return true if the provider is started
+     */
+    public boolean isStarted();    
+    /**
+     * Starts the instrumentation provider
+     */
+    public void stop();
+
+    /**
+     * Stops the instrumentation provider
+     */
+    public void start();
+
+    /**
+     * Gets the configuration associated with the instrumentation provider
+     * @return the configuration associated with the provider
+     */
+    public Configuration getConfiguration();
+    
+    /**
+     * Used to associate one or more instruments to a provider.  Instruments 
+     * are specified by class name or alias.  Multiple instruments must be
+     * specified as a comma separated list.
+     * 
+     * example:  DataCache,QueryCache,com.my.MyInstrument
+     *   where DataCache and QueryCache have aliases and com.my.MyInstrument is 
+     *   a class implementing an Instrument.
+     * 
+     * @param instruments  one or more instrument class names or aliases
+     */
+    public void setInstrument(String instruments);
+
+    /**
+     * Returns the string-based list of instruments directly configured by
+     * this provider via setInstrument.
+     * @return
+     */
+    public String getInstrument();
+ 
+    /**
+     * Sets configuration options for this provider
+     * @param options
+     */
+    public void setOptions(String options);
+    
+    /**
+     * Gets configuration options for this provider
+     * @param options
+     */
+    public String getOptions();
+    
+    /**
+     * Returns an string array of identifier to class name aliases for
+     * instruments known to the instrumentation provider.  Example:
+     * 
+     *  {"DataCache", "org.apache.openjpa.instrumentation.DataCacheInstrument",
+     *   "QueryCache", "org.apache.openjpa.instrumentation.QueryCacheInstrument"}
+     * @return a string array of identifier, class name pairs.
+     */
+    public String[] getInstrumentAliases();
+    
+    /**
+     * Adds an instrument to this providers list of managed instruments.  The
+     * instrument will participate in context-based lifecycle routines, 
+     * depending on the instrumentation level.
+     * @param instrument
+     */
+    public void addInstrument(Instrument instrument);
+    
+    /**
+     * Stops all instruments of the specified instrumentation level and context.
+     * @param level instrumentation level
+     * @param context  instrumentation context (factory, broker, config)
+     */
+    public void stopInstruments(InstrumentationLevel level, Object context);
+    
+    /**
+     * Starts all instruments of the specified instrumentation level and context.
+     * @param level instrumentation level
+     * @param context  instrumentation context (factory, broker, config)
+     */
+    public void startInstruments(InstrumentationLevel level, Object context);
+
+    /**
+     * Initializes an instrument within the provided context.
+     * @param instrument an instrument 
+     * @param context  instrumentation context (factory, broker, config)
+     */
+    public void initializeInstrument(Instrument instrument, Object context);
+
+    /**
+     * Initializes an instrument within the provided options and context.
+     * @param instrument an instrument 
+     * @param options configuration options to provide the instrument during initialization
+     * @param context  instrumentation context (factory, broker, config)
+     */
+    public void initializeInstrument(Instrument instrument, String options, Object context);
+
+    /**
+     * Returns an instrument instrumented by this provider by name
+     * @param name the name of the instrument to return
+     * @return the instrument or null if not instrumented by this provider
+     */
+    public Instrument getInstrumentByName(String name);
+
+    /**
+     * Removes an instrument instrumented by this provider by name
+     * @param name the name of the instrument to remove
+     */
+    public void removeInstrumentByName(String name);
+
+    /**
+     * Gets all instruments instrumented by this provider
+     * @return instruments instrumented by this provider
+     */
+    public Set<Instrument> getInstruments();
+
+    /**
+     * Starts an instrument
+     * @param instrument this instrument to start
+     */
+    public void startInstrument(Instrument instrument);
+    
+    /**
+     * Stops an instrument
+     * @param instrument the instrument to stop
+     */
+    public void stopInstrument(Instrument instrument);
+
+    /**
+     * Stops an instrument, forcing the stop, if necessary.
+     * @param instrument the instrument to stop
+     * @param force forces the stop if the instrument does not stop gracefully.
+     */
+    public void stopInstrument(Instrument instrument, boolean force);
+}

Propchange: openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/instrumentation/InstrumentationProvider.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/instrumentation/BrokerLevelInstrument.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/instrumentation/BrokerLevelInstrument.java?rev=981719&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/instrumentation/BrokerLevelInstrument.java
(added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/instrumentation/BrokerLevelInstrument.java
Tue Aug  3 01:55:02 2010
@@ -0,0 +1,60 @@
+/*
+ * 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.openjpa.instrumentation;
+
+import org.apache.openjpa.lib.instrumentation.AbstractInstrument;
+import org.apache.openjpa.lib.instrumentation.InstrumentationLevel;
+
+public class BrokerLevelInstrument extends AbstractInstrument {
+
+    public static String NAME = "NoneInstrument";
+    
+    private boolean _initialized; 
+    
+    @Override
+    public String getName() {
+        return NAME;
+    }
+
+    @Override
+    public void initialize() {
+        setInitialized(true);
+    }
+
+    public void start() {
+        setStarted(true);
+    }
+
+    public void stop() {
+        setStarted(false);
+    }
+
+    public InstrumentationLevel getLevel() {
+        return InstrumentationLevel.BROKER;
+    }
+
+    public void setInitialized(boolean _initialized) {
+        this._initialized = _initialized;
+    }
+
+    public boolean isInitialized() {
+        return _initialized;
+    }
+    
+}

Propchange: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/instrumentation/BrokerLevelInstrument.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/instrumentation/DCInstrument.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/instrumentation/DCInstrument.java?rev=981719&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/instrumentation/DCInstrument.java
(added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/instrumentation/DCInstrument.java
Tue Aug  3 01:55:02 2010
@@ -0,0 +1,44 @@
+/*
+ * 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.openjpa.instrumentation;
+
+import org.apache.openjpa.conf.OpenJPAConfiguration;
+
+public class DCInstrument extends AbstractDataCacheInstrument {
+
+    public static final String NAME = "SimpleDCInstrument";
+    
+    @Override
+    public String getName() {
+        return NAME;
+    }
+
+    public void initialize() {
+        OpenJPAConfiguration config = (OpenJPAConfiguration)getProvider().getConfiguration();
+        setDataCache(config.getDataCacheManagerInstance().getSystemDataCache());
+    }
+
+    public void start() {
+        setStarted(true);
+    }
+
+    public void stop() {
+        setStarted(false);
+    }    
+}

Propchange: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/instrumentation/DCInstrument.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/instrumentation/DynamicProvider.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/instrumentation/DynamicProvider.java?rev=981719&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/instrumentation/DynamicProvider.java
(added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/instrumentation/DynamicProvider.java
Tue Aug  3 01:55:02 2010
@@ -0,0 +1,51 @@
+/*
+ * 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.openjpa.instrumentation;
+
+import org.apache.openjpa.lib.instrumentation.AbstractInstrumentationProvider;
+import org.apache.openjpa.lib.instrumentation.Instrument;
+
+public class DynamicProvider extends AbstractInstrumentationProvider {
+
+    public static final String[] DYNAMIC_INSTRUMENT_ALIASES = { };
+    
+    public void start() {
+        setStarted(true);
+    }
+
+    public void stop() {
+        setStarted(false);
+        for (Instrument inst : getInstruments()) {
+           stopInstrument(inst);
+        }
+    }
+
+    public void startInstrument(Instrument instrument) {
+        instrument.start();
+    }
+
+    public void stopInstrument(Instrument instrument, boolean force) {
+        instrument.stop();
+    }
+    
+    @Override
+    public String[] getInstrumentAliases() {
+        return DYNAMIC_INSTRUMENT_ALIASES;
+    }
+}

Propchange: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/instrumentation/DynamicProvider.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/instrumentation/QCInstrument.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/instrumentation/QCInstrument.java?rev=981719&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/instrumentation/QCInstrument.java
(added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/instrumentation/QCInstrument.java
Tue Aug  3 01:55:02 2010
@@ -0,0 +1,44 @@
+/*
+ * 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.openjpa.instrumentation;
+
+import org.apache.openjpa.conf.OpenJPAConfiguration;
+
+public class QCInstrument extends AbstractQueryCacheInstrument {
+
+    public static final String NAME = "SimpleQCInstrument";
+    
+    @Override
+    public String getName() {
+        return NAME;
+    }
+
+    public void initialize() {
+        OpenJPAConfiguration config = (OpenJPAConfiguration)getProvider().getConfiguration();
+        setQueryCache(config.getDataCacheManagerInstance().getSystemQueryCache());
+    }
+
+    public void start() {
+        setStarted(true);
+    }
+
+    public void stop() {
+        setStarted(false);
+    }    
+}

Propchange: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/instrumentation/QCInstrument.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/instrumentation/QSCInstrument.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/instrumentation/QSCInstrument.java?rev=981719&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/instrumentation/QSCInstrument.java
(added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/instrumentation/QSCInstrument.java
Tue Aug  3 01:55:02 2010
@@ -0,0 +1,45 @@
+/*
+ * 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.openjpa.instrumentation;
+
+import org.apache.openjpa.conf.OpenJPAConfiguration;
+
+public class QSCInstrument extends AbstractPreparedQueryCacheInstrument {
+
+    public static final String NAME = "SimpleQSCInstrument";
+
+    @Override
+    public String getName() {
+        return NAME;
+    }
+
+    @Override
+    public void initialize() {
+        OpenJPAConfiguration config = (OpenJPAConfiguration)getProvider().getConfiguration();
+        setPreparedQueryCache(config.getQuerySQLCacheInstance());
+    }
+
+    public void start() {
+        setStarted(true);
+    }
+
+    public void stop() {
+        setStarted(false);
+    }    
+}

Propchange: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/instrumentation/QSCInstrument.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/instrumentation/SimpleProvider.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/instrumentation/SimpleProvider.java?rev=981719&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/instrumentation/SimpleProvider.java
(added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/instrumentation/SimpleProvider.java
Tue Aug  3 01:55:02 2010
@@ -0,0 +1,55 @@
+/*
+ * 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.openjpa.instrumentation;
+
+import org.apache.openjpa.lib.instrumentation.AbstractInstrumentationProvider;
+import org.apache.openjpa.lib.instrumentation.Instrument;
+
+public class SimpleProvider extends AbstractInstrumentationProvider {
+
+    public static final String[] SIMPLE_INSTRUMENT_ALIASES = {
+        "DataCache", "org.apache.openjpa.instrumentation.DCInstrument",
+        "QueryCache","org.apache.openjpa.instrumentation.QCInstrument",
+        "QuerySQLCache","org.apache.openjpa.instrumentation.QSCInstrument"
+    };
+    
+    public void start() {
+        setStarted(true);
+    }
+
+    public void stop() {
+        setStarted(false);
+        for (Instrument inst : getInstruments()) {
+           stopInstrument(inst);
+        }
+    }
+
+    public void startInstrument(Instrument instrument) {
+        instrument.start();
+    }
+
+    public void stopInstrument(Instrument instrument, boolean force) {
+        instrument.stop();
+    }
+    
+    @Override
+    public String[] getInstrumentAliases() {
+        return SIMPLE_INSTRUMENT_ALIASES;
+    }
+}

Propchange: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/instrumentation/SimpleProvider.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/instrumentation/TestInstrumentationProvider.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/instrumentation/TestInstrumentationProvider.java?rev=981719&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/instrumentation/TestInstrumentationProvider.java
(added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/instrumentation/TestInstrumentationProvider.java
Tue Aug  3 01:55:02 2010
@@ -0,0 +1,159 @@
+/*
+ * 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.openjpa.instrumentation;
+
+import java.util.Set;
+
+import javax.persistence.EntityManager;
+
+import org.apache.openjpa.lib.instrumentation.Instrument;
+import org.apache.openjpa.lib.instrumentation.InstrumentationProvider;
+import org.apache.openjpa.persistence.test.SingleEMFTestCase;
+
+/**
+ * Verifies the configuration and usage of a simple instrumentation
+ * provider.
+ * @author jrbauer
+ *
+ */
+public class TestInstrumentationProvider extends SingleEMFTestCase {
+
+    public static final String INSTRUMENTATION = 
+        "org.apache.openjpa.instrumentation.SimpleProvider(Instrument='DataCache,QueryCache,QuerySQLCache')";
+    
+    public void setUp() throws Exception {
+        super.setUp("openjpa.Instrumentation", 
+                    INSTRUMENTATION,
+                    "openjpa.DataCache",
+                    "true(EnableStatistics=true)",
+                    "openjpa.QueryCache",
+                    "true",
+                    "openjpa.RemoteCommitProvider",
+                    "sjvm");
+    }
+
+    /**
+     * Verifies simple instrumentation provider config with instruments defined
+     * for data cache and query cache.
+     */
+    public void testProviderConfig() {
+        // Verify an EMF was created with the supplied instrumentation
+        assertNotNull(emf);
+
+        // Verify the instrumentation value was stored in the config
+        String instrValue = emf.getConfiguration().getInstrumentation();
+        assertEquals(instrValue, INSTRUMENTATION);
+
+        // Verify an instrumentation manager is available
+        InstrumentationManager mgr = emf.getConfiguration().getInstrumentationManagerInstance();
+        assertNotNull(mgr);
+        
+        // Verify the manager is managing the correct provider
+        Set<InstrumentationProvider> providers = mgr.getProviders();
+        assertNotNull(providers);
+        assertEquals(1, providers.size());
+        InstrumentationProvider provider = providers.iterator().next();
+        assertEquals(provider.getClass(), SimpleProvider.class);
+        
+        // Verify the provider has instruments registered for the caches
+        Set<Instrument> instruments = provider.getInstruments();
+        assertNotNull(instruments);
+        assertEquals(3,instruments.size());
+        
+        // Lightweight verification of the instruments
+        Instrument inst = provider.getInstrumentByName(DCInstrument.NAME);
+        assertNotNull(inst);
+        assertTrue(inst instanceof DataCacheInstrument);
+        DataCacheInstrument dci = (DataCacheInstrument)inst;
+        assertEquals(dci.getCacheName(), "default");
+        inst = provider.getInstrumentByName(QCInstrument.NAME);
+        assertNotNull(inst);
+        assertTrue(inst instanceof QueryCacheInstrument);
+        inst = provider.getInstrumentByName(QSCInstrument.NAME);
+        assertNotNull(inst);
+        assertTrue(inst instanceof PreparedQueryCacheInstrument);
+    }
+    
+    /**
+     * Verifies configuring and adding an instrument to a provider after the provider
+     * has been initialized within the persistence unit. 
+     */
+    public void testDynamicInstrumentConfig() {
+        InstrumentationManager mgr = emf.getConfiguration().getInstrumentationManagerInstance();
+        assertNotNull(mgr);
+
+        Set<InstrumentationProvider> providers = mgr.getProviders();
+        assertNotNull(providers);
+        assertEquals(1, providers.size());
+        InstrumentationProvider provider = providers.iterator().next();
+        assertEquals(provider.getClass(), SimpleProvider.class);
+
+        verifyBrokerLevelInstrument(provider);
+    }
+
+    /**
+     * Verifies configuring and adding an instrumentation provider dynamically after
+     * the persistence unit has been created.
+     */
+    public void testDynamicProviderConfig() {
+        InstrumentationManager mgr = emf.getConfiguration().getInstrumentationManagerInstance();
+        assertNotNull(mgr);
+
+        DynamicProvider dyp = new DynamicProvider();
+        mgr.manageProvider(dyp);
+        verifyBrokerLevelInstrument(dyp);
+        assertTrue(dyp.isStarted());
+        assertNotNull(dyp.getInstrumentByName(BrokerLevelInstrument.NAME));
+        assertEquals(2, mgr.getProviders().size());
+    }
+
+    public void verifyBrokerLevelInstrument(InstrumentationProvider provider) {
+        // Create a new broker level instrument and register it with the
+        // provider
+        BrokerLevelInstrument bli = new BrokerLevelInstrument();
+        provider.addInstrument(bli);
+        // Verify instrument has not been initialized or started 
+        assertFalse(bli.isInitialized());
+        assertFalse(bli.isStarted());
+        
+        // Create a new EM/Broker
+        EntityManager em = emf.createEntityManager();
+        // Vfy the instrument has been initialized and started
+        assertTrue(bli.isInitialized());
+        assertTrue(bli.isStarted());
+        // Close the EM/Broker
+        em.close();
+        // Vfy the instrument has stopped
+        assertFalse(bli.isStarted());
+    }
+    
+    /**
+     * Verifies the cache metrics are available through simple instrumentation.
+     */
+//    public void testCacheInstruments() {
+//        
+//    }
+
+    /**
+     * Verifies multiple instrumentation providers can be specified.
+     */
+//    public void testMultipleProviderConfig() {
+//        
+//    }
+}

Propchange: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/instrumentation/TestInstrumentationProvider.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message