KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > joseki > server > ProcessorRegistry


1 /*
2  * (c) Copyright 2003, 2004 Hewlett-Packard Development Company, LP
3  * [See end of file]
4  */

5
6 package org.joseki.server;
7
8 import java.util.* ;
9
10 import org.joseki.vocabulary.*;
11
12 import com.hp.hpl.jena.rdf.model.* ;
13
14 /** Registry for operation processors.
15  *
16  * @author Andy Seaborne
17  * @version $Id: ProcessorRegistry.java,v 1.4 2004/04/30 14:13:13 andy_seaborne Exp $
18  */

19
20 public class ProcessorRegistry
21 {
22     QueryOperationRegistry queryRegistry = new QueryOperationRegistry() ;
23     OperationRegistry operationRegistry = new OperationRegistry() ;
24     
25     Set processorURIs = new HashSet() ;
26     Set queryLanguageURIs = new HashSet() ;
27     
28     public boolean hasProcessor(String JavaDoc opName)
29     {
30         return findProcessor( opName) != null ;
31     }
32     
33     public boolean hasQueryProcessor(String JavaDoc queryLanguageName)
34     {
35         return findQueryProcessor(queryLanguageName) != null ;
36     }
37     
38     
39     public Processor findProcessor(String JavaDoc opName)
40     {
41         return operationRegistry.findProcessor(opName) ;
42     }
43
44
45     public QueryProcessor findQueryProcessor(String JavaDoc queryLanguageName)
46     {
47         return queryRegistry.findProcessor(queryLanguageName) ;
48     }
49
50
51     public void registerProcessor(String JavaDoc shortName, Processor processor)
52     {
53         operationRegistry.register(shortName, processor) ;
54         processorURIs.add(processor.getInterfaceURI()) ;
55     }
56     
57     public void registerQueryProcessor(String JavaDoc langName, QueryProcessor queryProcessor)
58     {
59         queryRegistry.register(langName, queryProcessor) ;
60         queryLanguageURIs.add(queryProcessor.getInterfaceURI()) ;
61     }
62     
63     public void remove(Processor processor)
64     {
65         operationRegistry.remove(processor) ;
66     }
67
68     public void remove(QueryProcessor queryProcessor)
69     {
70         queryRegistry.remove(queryProcessor) ;
71     }
72     
73 // public Set getProcessorURIs() { return processorURIs ; }
74
// public Set getQueryLanguageURIs() { return queryLanguageURIs ; }
75

76     // Encode in RDF for OPTIONS
77

78
79     public void toRDF(Model optModel, Resource optionRes) throws RDFException
80     {
81         Map processors = operationRegistry.procRegistry ;
82         for ( Iterator iter = processors.keySet().iterator() ; iter.hasNext() ; )
83         {
84             String JavaDoc name = (String JavaDoc)iter.next() ;
85             Processor proc = (Processor)processors.get(name) ;
86             Resource r = optModel.createResource() ;
87             r.addProperty(JosekiVocab.operationName, name) ;
88             Resource opRes = optModel.createResource(proc.getInterfaceURI()) ;
89             r.addProperty(JosekiVocab.operation, opRes) ;
90             optModel.add(optionRes, JosekiVocab.hasOperation, r) ;
91         }
92
93         // Add query languages
94
Map queryLanguages = queryRegistry.procRegistry ;
95         for ( Iterator iter = queryLanguages.keySet().iterator() ; iter.hasNext() ; )
96         {
97             String JavaDoc qlName = (String JavaDoc)iter.next() ;
98             QueryProcessor qproc = (QueryProcessor)queryLanguages.get(qlName) ;
99             Resource r = optModel.createResource() ;
100             r.addProperty(JosekiVocab.queryOperationName, qlName) ;
101             Resource qRes = optModel.createResource(qproc.getInterfaceURI()) ;
102             r.addProperty(JosekiVocab.queryOperation, qRes) ;
103             optModel.add(optionRes, JosekiVocab.hasQueryOperation, r) ;
104         }
105
106     }
107
108     // ----------------------------------------------------
109
// The registry storage classes
110
// Wouldn't paramterized types be nice ?
111

112     static class OperationRegistry
113     {
114         // Map from short name to processor.
115
Map procRegistry = new HashMap() ;
116
117         void register(String JavaDoc shortName, Processor processor)
118         {
119             procRegistry.put(shortName, processor) ;
120         }
121     
122         Processor findProcessor(String JavaDoc opName)
123         {
124             Processor proc = (Processor)procRegistry.get(opName) ;
125             return proc ;
126         }
127
128
129     
130         void remove(Processor processor)
131         {
132             for ( Iterator iter = procRegistry.entrySet().iterator() ; iter.hasNext() ; )
133             {
134                 Map.Entry entry = (Map.Entry)iter.next() ;
135                 Processor proc = (Processor)entry.getValue() ;
136     
137                 if ( processor.equals(proc) )
138                     iter.remove() ;
139             }
140         }
141     }
142
143     static class QueryOperationRegistry
144     {
145         // Map from short name to processor.
146
Map procRegistry = new HashMap() ;
147
148         void register(String JavaDoc languageName, QueryProcessor queryPprocessor)
149         {
150             procRegistry.put(languageName, queryPprocessor) ;
151         }
152     
153         QueryProcessor findProcessor(String JavaDoc languageName)
154         {
155             QueryProcessor proc = (QueryProcessor)procRegistry.get(languageName) ;
156             return proc ;
157         }
158
159
160     
161         void remove(QueryProcessor queryProcessor)
162         {
163             for ( Iterator iter = procRegistry.entrySet().iterator() ; iter.hasNext() ; )
164             {
165                 Map.Entry entry = (Map.Entry)iter.next() ;
166                 QueryProcessor qProc = (QueryProcessor)entry.getValue() ;
167     
168                 if ( queryProcessor.equals(qProc) )
169                     iter.remove() ;
170             }
171         }
172     }
173 }
174
175 /*
176  * (c) Copyright 2003, 2004 Hewlett-Packard Development Company, LP
177  * All rights reserved.
178  *
179  * Redistribution and use in source and binary forms, with or without
180  * modification, are permitted provided that the following conditions
181  * are met:
182  * 1. Redistributions of source code must retain the above copyright
183  * notice, this list of conditions and the following disclaimer.
184  * 2. Redistributions in binary form must reproduce the above copyright
185  * notice, this list of conditions and the following disclaimer in the
186  * documentation and/or other materials provided with the distribution.
187  * 3. The name of the author may not be used to endorse or promote products
188  * derived from this software without specific prior written permission.
189  *
190  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
191  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
192  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
193  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
194  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
195  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
196  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
197  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
198  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
199  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
200  */

201
Popular Tags