Source Code Cross Referenced for DirectSolrConnection.java in  » Search-Engine » apache-solr-1.2.0 » org » apache » solr » servlet » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Search Engine » apache solr 1.2.0 » org.apache.solr.servlet 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         * Licensed to the Apache Software Foundation (ASF) under one or more
003:         * contributor license agreements.  See the NOTICE file distributed with
004:         * this work for additional information regarding copyright ownership.
005:         * The ASF licenses this file to You under the Apache License, Version 2.0
006:         * (the "License"); you may not use this file except in compliance with
007:         * the License.  You may obtain a copy of the License at
008:         *
009:         *     http://www.apache.org/licenses/LICENSE-2.0
010:         *
011:         * Unless required by applicable law or agreed to in writing, software
012:         * distributed under the License is distributed on an "AS IS" BASIS,
013:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         * See the License for the specific language governing permissions and
015:         * limitations under the License.
016:         */package org.apache.solr.servlet;
017:
018:        import java.io.File;
019:        import java.io.StringWriter;
020:        import java.util.ArrayList;
021:        import java.util.HashMap;
022:        import java.util.List;
023:
024:        import org.apache.solr.core.Config;
025:        import org.apache.solr.core.SolrConfig;
026:        import org.apache.solr.core.SolrCore;
027:        import org.apache.solr.core.SolrException;
028:        import org.apache.solr.request.MapSolrParams;
029:        import org.apache.solr.request.QueryResponseWriter;
030:        import org.apache.solr.request.SolrParams;
031:        import org.apache.solr.request.SolrQueryRequest;
032:        import org.apache.solr.request.SolrQueryResponse;
033:        import org.apache.solr.request.SolrRequestHandler;
034:        import org.apache.solr.schema.IndexSchema;
035:        import org.apache.solr.util.ContentStream;
036:        import org.apache.solr.util.ContentStreamBase;
037:
038:        /**
039:         * DirectSolrConnection provides an interface to solr that is similar to 
040:         * the the HTTP interface, but does not require an HTTP connection.
041:         * 
042:         * This class is designed to be as simple as possible and alow for more flexibility
043:         * in how you interface to solr.
044:         * 
045:         * @author ryan
046:         * @version $Id: DirectSolrConnection.java 542679 2007-05-29 22:28:21Z ryan $
047:         * @since solr 1.2
048:         */
049:        public class DirectSolrConnection {
050:            final SolrCore core;
051:            final SolrRequestParsers parser;
052:
053:            /**
054:             * Initialize using the static singleton SolrCore.getSolrCore().
055:             */
056:            public DirectSolrConnection() {
057:                core = SolrCore.getSolrCore();
058:                parser = new SolrRequestParsers(core, SolrConfig.config);
059:            }
060:
061:            /**
062:             * Initialize using an explicit SolrCore
063:             */
064:            public DirectSolrConnection(SolrCore c) {
065:                core = c;
066:                parser = new SolrRequestParsers(core, SolrConfig.config);
067:            }
068:
069:            /**
070:             * This constructor is designed to make it easy for JNI embedded applications 
071:             * to setup the entire solr environment with a simple interface.  It takes three parameters:
072:             * 
073:             * <code>instanceDir:</code> The solr instance directory.  If null, it will check the standard 
074:             * places first (JNDI,properties,"solr" directory)
075:             * 
076:             * <code>dataDir:</code> where the index is stored. 
077:             * 
078:             * <code>loggingPath:</code> Path to a java.util.logging.config.file.  If the path represents
079:             * an absolute path or is relative to the CWD, it will use that.  Next it will try a path 
080:             * relative to the instanceDir.  If none of these files exist, it will error.
081:             */
082:            public DirectSolrConnection(String instanceDir, String dataDir,
083:                    String loggingPath) {
084:                // If a loggingPath is specified, try using that (this needs to happen first)
085:                if (loggingPath != null) {
086:                    File loggingConfig = new File(loggingPath);
087:                    if (!loggingConfig.exists() && instanceDir != null) {
088:                        loggingConfig = new File(new File(instanceDir),
089:                                loggingPath);
090:                    }
091:                    if (loggingConfig.exists()) {
092:                        System.setProperty("java.util.logging.config.file",
093:                                loggingConfig.getAbsolutePath());
094:                    } else {
095:                        throw new SolrException(
096:                                SolrException.ErrorCode.SERVER_ERROR,
097:                                "can not find logging file: " + loggingConfig);
098:                    }
099:                }
100:
101:                // Set the instance directory
102:                if (instanceDir != null) {
103:                    if (Config.isInstanceDirInitialized()) {
104:                        String dir = Config.getInstanceDir();
105:                        if (!dir.equals(instanceDir)) {
106:                            throw new SolrException(
107:                                    SolrException.ErrorCode.SERVER_ERROR,
108:                                    "already initalized: " + dir);
109:                        }
110:                    }
111:                    Config.setInstanceDir(instanceDir);
112:                }
113:
114:                // If the Data directory is specified, initalize SolrCore directly
115:                if (dataDir != null) {
116:                    core = new SolrCore(dataDir, new IndexSchema(instanceDir
117:                            + "/conf/schema.xml"));
118:                } else {
119:                    core = SolrCore.getSolrCore();
120:                }
121:                parser = new SolrRequestParsers(core, SolrConfig.config);
122:            }
123:
124:            /**
125:             * For example:
126:             * 
127:             * String json = solr.request( "/select?qt=dismax&wt=json&q=...", null );
128:             * String xml = solr.request( "/update", "&lt;add><doc><field ..." );
129:             * 
130:             */
131:            public String request(String pathAndParams, String body)
132:                    throws Exception {
133:                String path = null;
134:                SolrParams params = null;
135:                int idx = pathAndParams.indexOf('?');
136:                if (idx > 0) {
137:                    path = pathAndParams.substring(0, idx);
138:                    params = SolrRequestParsers.parseQueryString(pathAndParams
139:                            .substring(idx + 1));
140:                } else {
141:                    path = pathAndParams;
142:                    params = new MapSolrParams(new HashMap<String, String>());
143:                }
144:
145:                // Extract the handler from the path or params
146:                SolrRequestHandler handler = core.getRequestHandler(path);
147:                if (handler == null) {
148:                    if ("/select".equals(path)
149:                            || "/select/".equalsIgnoreCase(path)) {
150:                        String qt = params.get(SolrParams.QT);
151:                        handler = core.getRequestHandler(qt);
152:                        if (handler == null) {
153:                            throw new SolrException(
154:                                    SolrException.ErrorCode.BAD_REQUEST,
155:                                    "unknown handler: " + qt);
156:                        }
157:                    }
158:                }
159:                if (handler == null) {
160:                    throw new SolrException(
161:                            SolrException.ErrorCode.BAD_REQUEST,
162:                            "unknown handler: " + path);
163:                }
164:
165:                // Make a stream for the 'body' content
166:                List<ContentStream> streams = new ArrayList<ContentStream>(1);
167:                if (body != null && body.length() > 0) {
168:                    streams.add(new ContentStreamBase.StringStream(body));
169:                }
170:
171:                SolrQueryRequest req = parser.buildRequestFrom(params, streams);
172:                SolrQueryResponse rsp = new SolrQueryResponse();
173:                core.execute(handler, req, rsp);
174:                if (rsp.getException() != null) {
175:                    throw rsp.getException();
176:                }
177:
178:                // Now write it out
179:                QueryResponseWriter responseWriter = core
180:                        .getQueryResponseWriter(req);
181:                StringWriter out = new StringWriter();
182:                responseWriter.write(out, req, rsp);
183:                return out.toString();
184:            }
185:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.