Source Code Cross Referenced for SimpleURLRegistry.java in  » ESB » synapse » org » apache » synapse » registry » url » 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 » ESB » synapse » org.apache.synapse.registry.url 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *  Licensed to the Apache Software Foundation (ASF) under one
003:         *  or more contributor license agreements.  See the NOTICE file
004:         *  distributed with this work for additional information
005:         *  regarding copyright ownership.  The ASF licenses this file
006:         *  to you under the Apache License, Version 2.0 (the
007:         *  "License"); you may not use this file except in compliance
008:         *  with the License.  You may obtain a copy of the License at
009:         *
010:         *   http://www.apache.org/licenses/LICENSE-2.0
011:         *
012:         *  Unless required by applicable law or agreed to in writing,
013:         *  software distributed under the License is distributed on an
014:         *   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015:         *  KIND, either express or implied.  See the License for the
016:         *  specific language governing permissions and limitations
017:         *  under the License.
018:         */
019:
020:        package org.apache.synapse.registry.url;
021:
022:        import org.apache.axiom.om.OMNode;
023:        import org.apache.axiom.om.impl.builder.StAXOMBuilder;
024:        import org.apache.commons.logging.Log;
025:        import org.apache.commons.logging.LogFactory;
026:        import org.apache.synapse.SynapseException;
027:        import org.apache.synapse.config.SynapseConfigUtils;
028:        import org.apache.synapse.registry.AbstractRegistry;
029:        import org.apache.synapse.registry.Registry;
030:        import org.apache.synapse.registry.RegistryEntry;
031:
032:        import javax.xml.stream.XMLInputFactory;
033:        import javax.xml.stream.XMLStreamException;
034:        import javax.xml.stream.XMLStreamReader;
035:        import java.io.*;
036:        import java.net.*;
037:        import java.util.ArrayList;
038:
039:        /**
040:         * A Simple HTTP GET based registry which will work with a Web Server / WebDAV
041:         * <p/>
042:         * This saves the root server URL, and appends the a given key to construct the
043:         * full URL to locate resources
044:         */
045:        public class SimpleURLRegistry extends AbstractRegistry implements 
046:                Registry {
047:
048:            private static final Log log = LogFactory
049:                    .getLog(SimpleURLRegistry.class);
050:
051:            private static final int MAX_KEYS = 200;
052:
053:            public OMNode lookup(String key) {
054:
055:                log.info("==> Repository fetch of resource with key : " + key);
056:                URLConnection urlc = null;
057:                try {
058:                    URL url = SynapseConfigUtils
059:                            .getURLFromPath(getRoot() + key);
060:                    if (url == null) {
061:                        return null;
062:                    }
063:                    urlc = url.openConnection();
064:                    urlc.connect();
065:                } catch (IOException e) {
066:                    return null;
067:                }
068:
069:                try {
070:                    XMLStreamReader parser = XMLInputFactory.newInstance()
071:                            .createXMLStreamReader(urlc.getInputStream());
072:                    StAXOMBuilder builder = new StAXOMBuilder(parser);
073:                    return builder.getDocumentElement();
074:
075:                } catch (MalformedURLException e) {
076:                    handleException("Invalid URL reference " + getRoot() + key,
077:                            e);
078:                } catch (FileNotFoundException fnf) {
079:                    return null;
080:                } catch (IOException e) {
081:                    handleException("IO Error reading from URL " + getRoot()
082:                            + key, e);
083:                } catch (XMLStreamException e) {
084:                    handleException("XML Error reading from URL " + getRoot()
085:                            + key, e);
086:                }
087:                return null;
088:            }
089:
090:            public RegistryEntry getRegistryEntry(String key) {
091:                if (log.isDebugEnabled()) {
092:                    log.debug("Perform RegistryEntry lookup for key : " + key);
093:                }
094:                try {
095:                    URL url = SynapseConfigUtils
096:                            .getURLFromPath(getRoot() + key);
097:                    if (url == null) {
098:                        return null;
099:                    }
100:                    URLConnection urlc = url.openConnection();
101:                    urlc.setReadTimeout(30000);
102:                    urlc.setRequestProperty("Connection", "Close");
103:
104:                    URLRegistryEntry wre = new URLRegistryEntry();
105:                    wre.setKey(key);
106:                    wre.setName(url.getFile());
107:                    wre.setType(new URI(urlc.getContentType()));
108:                    wre.setDescription("Resource at : " + url.toString());
109:                    wre.setLastModified(urlc.getLastModified());
110:                    wre.setVersion(urlc.getLastModified());
111:                    if (urlc.getExpiration() > 0) {
112:                        wre.setCachableDuration(urlc.getExpiration()
113:                                - System.currentTimeMillis());
114:                    } else {
115:                        wre.setCachableDuration(getCachableDuration());
116:                    }
117:                    return wre;
118:
119:                } catch (MalformedURLException e) {
120:                    handleException("Invalid URL reference " + getRoot() + key,
121:                            e);
122:                } catch (IOException e) {
123:                    handleException("IO Error reading from URL " + getRoot()
124:                            + key, e);
125:                } catch (URISyntaxException e) {
126:                    handleException("URI Syntax error reading from URL "
127:                            + getRoot() + key, e);
128:                }
129:                return null;
130:            }
131:
132:            public void addConfigProperty(String name, String value) {
133:
134:                if (name.equals("root")) {
135:
136:                    // if the root is folder, it should always end with '/'
137:                    // therefore, property keys do not have to begin with '/', which could be misleading
138:                    try {
139:                        URL url = new URL(value);
140:                        if (url.getProtocol().equals("file")) {
141:                            if (!value.endsWith("/")) {
142:                                value = value + "/";
143:                            }
144:                        }
145:                    } catch (MalformedURLException e) {
146:                        // don't do any thing if this is not a valid URL
147:                    }
148:                }
149:
150:                super .addConfigProperty(name, value);
151:            }
152:
153:            public String getRoot() {
154:                String root = (String) properties.get("root");
155:                if (root == null) {
156:                    return "";
157:                } else {
158:                    return root;
159:                }
160:            }
161:
162:            public long getCachableDuration() {
163:                String cachableDuration = (String) properties
164:                        .get("cachableDuration");
165:                return cachableDuration == null ? 1500 : Long
166:                        .parseLong(cachableDuration);
167:            }
168:
169:            public RegistryEntry[] getChildren(RegistryEntry entry) {
170:                URL url;
171:                if (entry == null) {
172:                    URLRegistryEntry urlEntry = new URLRegistryEntry();
173:                    urlEntry.setKey("");
174:                    entry = urlEntry;
175:                }
176:                url = SynapseConfigUtils.getURLFromPath(getRoot()
177:                        + entry.getKey());
178:                if (url == null) {
179:                    return null;
180:                }
181:                if (url.getProtocol().equals("file")) {
182:
183:                    File file = new File(url.getFile());
184:                    if (!file.isDirectory()) {
185:                        return null;
186:                    }
187:                    InputStream inStream = null;
188:                    try {
189:                        inStream = (InputStream) url.getContent();
190:
191:                        BufferedReader reader = new BufferedReader(
192:                                new InputStreamReader(inStream));
193:                        ArrayList entryList = new ArrayList();
194:                        String key = "";
195:                        while ((key = reader.readLine()) != null) {
196:                            URLRegistryEntry registryEntry = new URLRegistryEntry();
197:                            if (entry.getKey().equals("")) {
198:                                registryEntry.setKey(key);
199:                            } else {
200:                                if (entry.getKey().endsWith("/")) {
201:                                    registryEntry.setKey(entry.getKey() + key);
202:                                } else {
203:                                    registryEntry.setKey(entry.getKey() + "/"
204:                                            + key);
205:                                }
206:                            }
207:
208:                            entryList.add(registryEntry);
209:                        }
210:
211:                        RegistryEntry[] entries = new RegistryEntry[entryList
212:                                .size()];
213:                        for (int i = 0; i < entryList.size(); i++) {
214:                            entries[i] = (RegistryEntry) entryList.get(i);
215:                        }
216:                        return entries;
217:
218:                    } catch (Exception e) {
219:                        throw new SynapseException("Error in reading the URL.");
220:                    }
221:
222:                } else {
223:                    throw new SynapseException("Invalid protocol.");
224:                }
225:            }
226:
227:            public RegistryEntry[] getDescendants(RegistryEntry entry) {
228:
229:                ArrayList list = new ArrayList();
230:                RegistryEntry[] entries = getChildren(entry);
231:                if (entries != null) {
232:                    for (int i = 0; i < entries.length; i++) {
233:
234:                        if (list.size() > MAX_KEYS) {
235:                            break;
236:                        }
237:
238:                        fillDescendants(entries[i], list);
239:                    }
240:                }
241:
242:                RegistryEntry[] descendants = new RegistryEntry[list.size()];
243:                for (int i = 0; i < list.size(); i++) {
244:                    descendants[i] = (RegistryEntry) list.get(i);
245:                }
246:
247:                return descendants;
248:            }
249:
250:            private void fillDescendants(RegistryEntry parent, ArrayList list) {
251:
252:                RegistryEntry[] entries = getChildren(parent);
253:                if (entries != null) {
254:                    for (int i = 0; i < entries.length; i++) {
255:
256:                        if (list.size() > MAX_KEYS) {
257:                            break;
258:                        }
259:
260:                        fillDescendants(entries[i], list);
261:                    }
262:                } else {
263:                    list.add(parent);
264:                }
265:            }
266:
267:            private void handleException(String msg, Exception e) {
268:                log.error(msg, e);
269:                throw new SynapseException(msg, e);
270:            }
271:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.