001: //=============================================================================
002: //=== Copyright (C) 2001-2007 Food and Agriculture Organization of the
003: //=== United Nations (FAO-UN), United Nations World Food Programme (WFP)
004: //=== and United Nations Environment Programme (UNEP)
005: //===
006: //=== This program is free software; you can redistribute it and/or modify
007: //=== it under the terms of the GNU General Public License as published by
008: //=== the Free Software Foundation; either version 2 of the License, or (at
009: //=== your option) any later version.
010: //===
011: //=== This program is distributed in the hope that it will be useful, but
012: //=== WITHOUT ANY WARRANTY; without even the implied warranty of
013: //=== MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: //=== General Public License for more details.
015: //===
016: //=== You should have received a copy of the GNU General Public License
017: //=== along with this program; if not, write to the Free Software
018: //=== Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
019: //===
020: //=== Contact: Jeroen Ticheler - FAO - Viale delle Terme di Caracalla 2,
021: //=== Rome - Italy. email: geonetwork@osgeo.org
022: //==============================================================================
023:
024: package org.fao.gast.lib;
025:
026: import jeeves.constants.ConfigFile;
027: import jeeves.interfaces.Activator;
028: import jeeves.server.resources.ProviderManager;
029: import jeeves.server.resources.ResourceManager;
030: import org.jdom.Element;
031:
032: //=============================================================================
033:
034: public class Resource {
035: //--------------------------------------------------------------------------
036: //---
037: //--- Constructor
038: //---
039: //--------------------------------------------------------------------------
040:
041: public Resource(String appPath, Element resource) throws Exception {
042: name = resource.getChildText(ConfigFile.Resource.Child.NAME);
043: String provider = resource
044: .getChildText(ConfigFile.Resource.Child.PROVIDER);
045: Element config = resource
046: .getChild(ConfigFile.Resource.Child.CONFIG);
047: Element activator = resource
048: .getChild(ConfigFile.Resource.Child.ACTIVATOR);
049:
050: if (activator != null) {
051: String clas = activator
052: .getAttributeValue(ConfigFile.Activator.Attr.CLASS);
053:
054: activ = (Activator) Class.forName(clas).newInstance();
055: activ.startup(appPath, activator);
056: }
057:
058: try {
059: provMan.register(provider, name, config);
060: } catch (Exception e) {
061: //--- in case of error we have to stop the activator
062:
063: provMan.end();
064:
065: if (activ != null)
066: activ.shutdown();
067:
068: throw e;
069: }
070:
071: resMan = new ResourceManager(provMan);
072: }
073:
074: //--------------------------------------------------------------------------
075: //---
076: //--- API methods
077: //---
078: //--------------------------------------------------------------------------
079:
080: public Object open() throws Exception {
081: return resMan.open(name);
082: }
083:
084: //--------------------------------------------------------------------------
085:
086: public void close() {
087: try {
088: resMan.close();
089: } catch (Exception e) {
090: e.printStackTrace();
091: doAbort();
092: }
093:
094: provMan.end();
095:
096: if (activ != null)
097: activ.shutdown();
098: }
099:
100: //--------------------------------------------------------------------------
101:
102: public void abort() {
103: doAbort();
104:
105: provMan.end();
106:
107: if (activ != null)
108: activ.shutdown();
109: }
110:
111: //--------------------------------------------------------------------------
112: //---
113: //--- Private methods
114: //---
115: //--------------------------------------------------------------------------
116:
117: private void doAbort() {
118: try {
119: resMan.abort();
120: } catch (Exception e) {
121: e.printStackTrace();
122: }
123: }
124:
125: //--------------------------------------------------------------------------
126: //---
127: //--- Variables
128: //---
129: //--------------------------------------------------------------------------
130:
131: private String name;
132: private Activator activ;
133: private ResourceManager resMan;
134: private ProviderManager provMan = new ProviderManager();
135: }
136:
137: //=============================================================================
|