Source Code Cross Referenced for StandardPathResolver.java in  » Development » Java-Plugin-Framework » org » java » plugin » standard » 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 » Development » Java Plugin Framework » org.java.plugin.standard 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*****************************************************************************
002:         * Java Plug-in Framework (JPF)
003:         * Copyright (C) 2004-2007 Dmitry Olshansky
004:         * 
005:         * This library is free software; you can redistribute it and/or
006:         * modify it under the terms of the GNU Lesser General Public
007:         * License as published by the Free Software Foundation; either
008:         * version 2.1 of the License, or (at your option) any later version.
009:         * 
010:         * This library is distributed in the hope that it will be useful,
011:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
012:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013:         * Lesser General Public License for more details.
014:         * 
015:         * You should have received a copy of the GNU Lesser General Public
016:         * License along with this library; if not, write to the Free Software
017:         * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018:         *****************************************************************************/package org.java.plugin.standard;
019:
020:        import java.io.File;
021:        import java.net.MalformedURLException;
022:        import java.net.URL;
023:        import java.util.HashMap;
024:        import java.util.Locale;
025:        import java.util.Map;
026:
027:        import org.apache.commons.logging.Log;
028:        import org.apache.commons.logging.LogFactory;
029:        import org.java.plugin.PathResolver;
030:        import org.java.plugin.registry.Identity;
031:        import org.java.plugin.registry.PluginDescriptor;
032:        import org.java.plugin.registry.PluginElement;
033:        import org.java.plugin.registry.PluginFragment;
034:        import org.java.plugin.util.ExtendedProperties;
035:        import org.java.plugin.util.IoUtil;
036:
037:        /**
038:         * Standard simple implementation of path resolver. For resolving it uses
039:         * plug-in element registration (see {@link #registerContext(Identity, URL)})
040:         * procedure.
041:         * @version $Id$
042:         */
043:        public class StandardPathResolver implements  PathResolver {
044:            protected Log log = LogFactory.getLog(getClass());
045:            private Map<String, URL> urlMap = new HashMap<String, URL>();
046:
047:            /**
048:             * This implementation accepts {@link PluginDescriptor} or
049:             * {@link PluginFragment} as valid plug-in elements.
050:             * @see org.java.plugin.PathResolver#registerContext(
051:             *      org.java.plugin.registry.Identity, java.net.URL)
052:             */
053:            public void registerContext(final Identity idt, final URL url) {
054:                if (!(idt instanceof  PluginDescriptor)
055:                        && !(idt instanceof  PluginFragment)) {
056:                    throw new IllegalArgumentException(
057:                            "unsupported identity class " //$NON-NLS-1$
058:                                    + idt.getClass().getName());
059:                }
060:                final URL oldUrl = urlMap.put(idt.getId(), url);
061:                if (oldUrl != null) {
062:                    log.warn("old context URL " + oldUrl //$NON-NLS-1$
063:                            + " has been replaced with new " + url //$NON-NLS-1$
064:                            + " for " + idt //$NON-NLS-1$
065:                            + " with key " + idt.getId()); //$NON-NLS-1$
066:                } else {
067:                    if (log.isDebugEnabled()) {
068:                        log.debug("context URL " + url //$NON-NLS-1$
069:                                + " registered for " + idt //$NON-NLS-1$
070:                                + " with key " + idt.getId()); //$NON-NLS-1$
071:                    }
072:                }
073:            }
074:
075:            /**
076:             * @see org.java.plugin.PathResolver#unregisterContext(java.lang.String)
077:             */
078:            public void unregisterContext(final String id) {
079:                final URL url = urlMap.remove(id);
080:                if (url == null) {
081:                    log.warn("no context was registered with key " + id); //$NON-NLS-1$
082:                } else {
083:                    if (log.isDebugEnabled()) {
084:                        log.debug("context URL " + url //$NON-NLS-1$
085:                                + " un-registered for key " + id); //$NON-NLS-1$
086:                    }
087:                }
088:            }
089:
090:            /**
091:             * @see org.java.plugin.PathResolver#resolvePath(
092:             *      org.java.plugin.registry.Identity, java.lang.String)
093:             */
094:            public URL resolvePath(final Identity identity, final String path) {
095:                URL baseUrl;
096:                if ((identity instanceof  PluginDescriptor)
097:                        || (identity instanceof  PluginFragment)) {
098:                    baseUrl = getRegisteredContext(identity.getId());
099:                } else if (identity instanceof  PluginElement) {
100:                    PluginElement<?> element = (PluginElement) identity;
101:                    if (element.getDeclaringPluginFragment() != null) {
102:                        baseUrl = getRegisteredContext(element
103:                                .getDeclaringPluginFragment().getId());
104:                    } else {
105:                        baseUrl = getRegisteredContext(element
106:                                .getDeclaringPluginDescriptor().getId());
107:                    }
108:                } else {
109:                    throw new IllegalArgumentException(
110:                            "unknown identity class " //$NON-NLS-1$
111:                                    + identity.getClass().getName());
112:                }
113:                return resolvePath(baseUrl, path);
114:            }
115:
116:            /**
117:             * @see org.java.plugin.PathResolver#getRegisteredContext(java.lang.String)
118:             */
119:            public URL getRegisteredContext(final String id) {
120:                final URL result = urlMap.get(id);
121:                if (result == null) {
122:                    throw new IllegalArgumentException("unknown plug-in or" //$NON-NLS-1$
123:                            + " plug-in fragment ID - " + id); //$NON-NLS-1$
124:                }
125:                return result;
126:            }
127:
128:            /**
129:             * @see org.java.plugin.PathResolver#isContextRegistered(java.lang.String)
130:             */
131:            public boolean isContextRegistered(final String id) {
132:                return urlMap.containsKey(id);
133:            }
134:
135:            /**
136:             * Resolves given path against given base URL.
137:             * @param baseUrl base URL to resolve given path
138:             * @param path path to be resolved
139:             * @return resolved URL
140:             */
141:            protected URL resolvePath(final URL baseUrl, final String path) {
142:                try {
143:                    if ("".equals(path) || "/".equals(path)) { //$NON-NLS-1$ //$NON-NLS-2$
144:                        return maybeJarUrl(baseUrl);
145:                    }
146:                    return maybeJarUrl(new URL(maybeJarUrl(baseUrl), path));
147:                } catch (MalformedURLException mue) {
148:                    log.error("can't create URL in context of " + baseUrl //$NON-NLS-1$
149:                            + " and path " + path, mue); //$NON-NLS-1$
150:                    throw new IllegalArgumentException("path " + path //$NON-NLS-1$
151:                            + " in context of " + baseUrl //$NON-NLS-1$
152:                            + " cause creation of malformed URL"); //$NON-NLS-1$
153:                }
154:            }
155:
156:            protected URL maybeJarUrl(final URL url)
157:                    throws MalformedURLException {
158:                if ("jar".equalsIgnoreCase(url.getProtocol())) { //$NON-NLS-1$
159:                    return url;
160:                }
161:                File file = IoUtil.url2file(url);
162:                if ((file == null) || !file.isFile()) {
163:                    return url;
164:                }
165:                String fileName = file.getName().toLowerCase(
166:                        Locale.getDefault());
167:                if (fileName.endsWith(".jar") //$NON-NLS-1$
168:                        || fileName.endsWith(".zip")) { //$NON-NLS-1$
169:                    return new URL("jar:" //$NON-NLS-1$
170:                            + IoUtil.file2url(file).toExternalForm() + "!/"); //$NON-NLS-1$
171:                }
172:                return url;
173:            }
174:
175:            /**
176:             * No configuration parameters expected in this implementation.
177:             * @see org.java.plugin.PathResolver#configure(ExtendedProperties)
178:             */
179:            public void configure(final ExtendedProperties config)
180:                    throws Exception {
181:                // no-op
182:            }
183:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.