001: /*****************************************************************************
002: * Java Plug-in Framework (JPF)
003: * Copyright (C) 2004-2007 Dmitry Olshansky
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: *****************************************************************************/package org.java.plugin;
019:
020: import org.apache.commons.logging.Log;
021: import org.apache.commons.logging.LogFactory;
022: import org.java.plugin.registry.PluginDescriptor;
023:
024: /**
025: * This is base for "home" class of plug-in runtime. Using this class,
026: * plug-in code can get access to plug-in framework
027: * ({@link org.java.plugin.PluginManager manager},
028: * {@link org.java.plugin.registry.PluginRegistry registry}) which was loaded it.
029: * It is also used by manager during plug-in life cycle management (activation
030: * and deactivation).
031: * <br>
032: * Plug-in vendor may provide it's own implementation of this class if some
033: * actions should be performed during plug-in activation/deactivation. When no
034: * class specified, framework provides default "empty" implementation that does
035: * nothing when plug-in started and stopped.
036: * @version $Id$
037: */
038: public abstract class Plugin {
039: /**
040: * Makes logging service available for descending classes.
041: */
042: protected final Log log = LogFactory.getLog(getClass());
043:
044: private PluginManager manager;
045: private PluginDescriptor descriptor;
046: private boolean started;
047:
048: /**
049: * @return descriptor of this plug-in
050: */
051: public final PluginDescriptor getDescriptor() {
052: return descriptor;
053: }
054:
055: /*
056: * For internal use only!
057: */
058: final void setDescriptor(final PluginDescriptor descr) {
059: this .descriptor = descr;
060: }
061:
062: /**
063: * @return manager which controls this plug-in
064: */
065: public final PluginManager getManager() {
066: return manager;
067: }
068:
069: /*
070: * For internal use only!
071: */
072: final void setManager(final PluginManager aManager) {
073: this .manager = aManager;
074: }
075:
076: /*
077: * For internal use only!
078: */
079: final void start() throws Exception {
080: if (!started) {
081: doStart();
082: started = true;
083: }
084: }
085:
086: /*
087: * For internal use only!
088: */
089: final void stop() throws Exception {
090: if (started) {
091: doStop();
092: started = false;
093: }
094: }
095:
096: /**
097: * @return <code>true</code> if this plug-in is in active state
098: */
099: public final boolean isActive() {
100: return started;
101: }
102:
103: /**
104: * This method will be called once during plug-in activation before any
105: * access to any code from this plug-in.
106: * @throws Exception if an error has occurred during plug-in start-up
107: */
108: protected abstract void doStart() throws Exception;
109:
110: /**
111: * This method will be called once during plug-in deactivation. After
112: * this method call, no other code from this plug-in can be accessed,
113: * unless {@link #doStart()} method will be called again (but for another
114: * instance of this class).
115: * @throws Exception if an error has occurred during plug-in shutdown
116: */
117: protected abstract void doStop() throws Exception;
118:
119: /**
120: * @see java.lang.Object#toString()
121: */
122: @Override
123: public String toString() {
124: return "{" + getClass().getName() //$NON-NLS-1$
125: + ": manager=" + manager //$NON-NLS-1$
126: + ", descriptor=" + descriptor //$NON-NLS-1$
127: + "}"; //$NON-NLS-1$
128: }
129: }
|