001: /*
002: * Copyright (C) 1999-2004 <a href="mailto:mandarax@jbdietrich.com">Jens Dietrich</a>
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package org.mandarax.jdbc.server;
020:
021: import java.io.FileInputStream;
022: import java.io.IOException;
023: import java.io.InputStream;
024: import java.util.StringTokenizer;
025:
026: import org.mandarax.jdbc.JDBCUtils;
027: import org.mandarax.kernel.KnowledgeBase;
028:
029: /**
030: * The default knowledge base server, based on associations between names
031: * and mandarax xkb and zkb drivers.
032: * @author <A HREF="mailto:mandarax@jbdietrich.com">Jens Dietrich</A>
033: * @version 3.3.2 <29 December 2004>
034: * @since 3.0
035: */
036:
037: public class DefaultKnowledgeBaseServer extends KnowledgeBaseServer {
038:
039: // new protocol handlers should be registered here
040: private static AbstractKnowledgeBaseFactory[] kbFactories = {
041: new ZKBKnowledgeBaseFactory(),
042: new XKBKnowledgeBaseFactory(),
043: new RuleMLKnowledgeBaseFactory(),
044: new KnowledgeBaseFactoryProxy(),
045: new KnowledgeBaseRegistry() };
046:
047: /**
048: * Get the knowledge base for the given url.
049: * @return a knowledge base
050: * @param url an URL
051: */
052: public KnowledgeBase getKnowledgeBase(String url)
053: throws IllegalArgumentException,
054: CannotLoadKnowledgeBaseException {
055:
056: if (url == null)
057: throw new IllegalArgumentException("URL should not be null");
058: if (!url.startsWith(JDBCUtils.URL_PREFIX
059: + JDBCUtils.URL_SEPARATOR))
060: throw new IllegalArgumentException(
061: "Valid URLs must start with "
062: + JDBCUtils.URL_PREFIX);
063:
064: StringTokenizer tokenizer = new StringTokenizer(url,
065: JDBCUtils.URL_SEPARATOR);
066:
067: tokenizer.nextToken();
068: tokenizer.nextToken();
069: String protocol = tokenizer.hasMoreTokens() ? tokenizer
070: .nextToken() : null;
071: String source = tokenizer.hasMoreTokens() ? tokenizer
072: .nextToken() : null;
073:
074: if (protocol == null)
075: throw new IllegalArgumentException("Invalid URL " + url
076: + " - protocol token missing");
077: if (source == null)
078: throw new IllegalArgumentException("Invalid URL " + url
079: + " - kb source token missing");
080: else {
081: // e.g., to support filenames line c:/ using the SEPARATOR
082: while (tokenizer.hasMoreTokens())
083: source = source + JDBCUtils.URL_SEPARATOR
084: + tokenizer.nextToken();
085: }
086: return getKnowledgeBase(url, protocol, source);
087: }
088:
089: /**
090: * Get the knowledge base for the given url, protocol and source.
091: * @return a knowledge base
092: * @param url an URL
093: * @param protocol a protocol (name)
094: * @param source a source (file name, file url)
095: */
096: protected KnowledgeBase getKnowledgeBase(String url,
097: String protocol, String source)
098: throws IllegalArgumentException,
099: CannotLoadKnowledgeBaseException {
100: String PROTOCOL = normalizeProtocolName(protocol);
101: KnowledgeBaseFactory kbFactory = null;
102: int i = 0;
103: while (i < kbFactories.length && kbFactory == null) {
104: if (kbFactories[i].supportsProtocol(PROTOCOL))
105: kbFactory = kbFactories[i];
106: i = i + 1;
107: }
108: if (kbFactory == null)
109: throw new CannotLoadKnowledgeBaseException(
110: "No kb factory registered for protocol " + PROTOCOL);
111: else {
112: try {
113: return kbFactory.getKnowledgeBase(source);
114: } catch (Exception x) {
115: throw new CannotLoadKnowledgeBaseException(
116: "Cannot load knowledge base from " + source
117: + "with protocol " + PROTOCOL, x);
118: }
119: }
120: }
121:
122: /**
123: * Get the input stream from where to load the resource.
124: * @param source
125: */
126: protected InputStream getInputStream(String source)
127: throws IllegalArgumentException,
128: CannotLoadKnowledgeBaseException {
129: if (source == null)
130: throw new IllegalArgumentException(
131: "Cannot load knowledge base from null source");
132: try {
133: return new FileInputStream(source);
134: } catch (IOException x) {
135: throw new CannotLoadKnowledgeBaseException(
136: "Cannot open file " + source);
137: }
138:
139: }
140:
141: /**
142: * Normalize a protocol string.
143: * @param protocol a string
144: * @return a normalized string
145: */
146: private String normalizeProtocolName(String protocol) {
147: return protocol == null ? null : protocol.trim().toLowerCase();
148: }
149: }
|