Source Code Cross Referenced for BundleUtil.java in  » IDE-Eclipse » ui » org » eclipse » ui » internal » intro » universal » util » 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 » IDE Eclipse » ui » org.eclipse.ui.internal.intro.universal.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*******************************************************************************
002:         * Copyright (c) 2004, 2006 IBM Corporation and others.
003:         * All rights reserved. This program and the accompanying materials
004:         * are made available under the terms of the Eclipse Public License v1.0
005:         * which accompanies this distribution, and is available at
006:         * http://www.eclipse.org/legal/epl-v10.html
007:         *
008:         * Contributors:
009:         *     IBM Corporation - initial API and implementation
010:         *******************************************************************************/package org.eclipse.ui.internal.intro.universal.util;
011:
012:        import java.io.IOException;
013:        import java.net.URL;
014:
015:        import org.eclipse.core.runtime.FileLocator;
016:        import org.eclipse.core.runtime.IConfigurationElement;
017:        import org.eclipse.core.runtime.IPath;
018:        import org.eclipse.core.runtime.Path;
019:        import org.eclipse.core.runtime.Platform;
020:        import org.osgi.framework.Bundle;
021:        import org.osgi.framework.Constants;
022:
023:        /**
024:         * Bundle convenience methods.
025:         */
026:        public class BundleUtil {
027:
028:            private static String NL_TAG = "$nl$/"; //$NON-NLS-1$
029:
030:            /**
031:             * Utility method to validate the state of a bundle. Log invalid bundles to
032:             * log file.
033:             */
034:            public static boolean bundleHasValidState(Bundle bundle) {
035:                if (bundle == null || bundle.getState() == Bundle.UNINSTALLED
036:                        || bundle.getState() == Bundle.INSTALLED) {
037:
038:                    if (bundle == null)
039:                        Log
040:                                .error(
041:                                        "Universal Welcome tried accessing a NULL bundle.", null); //$NON-NLS-1$
042:                    else {
043:                        String msg = StringUtil
044:                                .concat(
045:                                        "Universal Welcome tried accessing Bundle: ", getBundleHeader( //$NON-NLS-1$
046:                                                bundle, Constants.BUNDLE_NAME),
047:                                        " vendor: ", //$NON-NLS-1$
048:                                        getBundleHeader(bundle,
049:                                                Constants.BUNDLE_VENDOR),
050:                                        " bundle state: ", String.valueOf(bundle.getState())).toString(); //$NON-NLS-1$
051:                        Log.error(msg, null);
052:                    }
053:                    return false;
054:                }
055:
056:                return true;
057:            }
058:
059:            /**
060:             * Retrieves the given key from the bundle header.
061:             * 
062:             * @param bundle
063:             * @param key
064:             * @return
065:             */
066:            public static String getBundleHeader(Bundle bundle, String key) {
067:                return (String) bundle.getHeaders().get(key);
068:            }
069:
070:            public static Bundle getBundleFromConfigurationElement(
071:                    IConfigurationElement cfg) {
072:                return Platform.getBundle(cfg.getNamespaceIdentifier());
073:            }
074:
075:            /**
076:             * Get the resourcelocation, but do not force an $nl$ on it.
077:             * 
078:             * @param resource
079:             * @param element
080:             * @return
081:             */
082:            public static String getResourceLocation(String resource,
083:                    IConfigurationElement element) {
084:                Bundle bundle = getBundleFromConfigurationElement(element);
085:                return getResolvedResourceLocation(resource, bundle, false);
086:            }
087:
088:            /**
089:             * Returns the fully qualified location of the passed resource string from
090:             * the passed plugin id. If the file could not be loaded from the plugin,
091:             * the resource is returned as is.
092:             * 
093:             * @param resource
094:             * @return
095:             */
096:            public static String getResolvedResourceLocation(String resource,
097:                    String pluginId) {
098:                Bundle bundle = Platform.getBundle(pluginId);
099:                return getResolvedResourceLocation(resource, bundle, true);
100:            }
101:
102:            /**
103:             * Shorthand util method.
104:             * 
105:             * @param resource
106:             * @return
107:             */
108:            public static String getResolvedResourceLocation(String resource,
109:                    Bundle bundle) {
110:                return getResolvedResourceLocation(resource, bundle, true);
111:            }
112:
113:            public static String getResolvedResourceLocation(String base,
114:                    String resource, Bundle bundle) {
115:                // quick exits.
116:                if (resource == null)
117:                    return null;
118:
119:                String fullResource = new Path(base).append(resource)
120:                        .toString();
121:                String resolvedResource = getResolvedResourceLocation(
122:                        fullResource, bundle, true);
123:
124:                if (resolvedResource.equals(fullResource))
125:                    // return resource as is when the resource does not exist.
126:                    return resource;
127:                return resolvedResource;
128:            }
129:
130:            public static String getResolvedResourceLocation(String resource,
131:                    Bundle bundle, boolean forceNLResolve) {
132:                // quick exits.
133:                if (resource == null)
134:                    return null;
135:
136:                if (bundle == null || !bundleHasValidState(bundle))
137:                    return resource;
138:
139:                URL localLocation = null;
140:                try {
141:                    // we need to resolve this URL.
142:                    String copyResource = resource;
143:                    if (forceNLResolve && !copyResource.startsWith(NL_TAG)) {
144:                        if (copyResource.startsWith("/") //$NON-NLS-1$
145:                                || copyResource.startsWith("\\")) //$NON-NLS-1$
146:                            copyResource = resource.substring(1);
147:                        copyResource = NL_TAG + copyResource;
148:                    }
149:                    IPath resourcePath = new Path(copyResource);
150:                    localLocation = FileLocator
151:                            .find(bundle, resourcePath, null);
152:                    if (localLocation == null) {
153:                        // localLocation can be null if the passed resource could not
154:                        // be found relative to the plugin. log fact, return resource,
155:                        // as is.
156:                        String msg = StringUtil.concat(
157:                                "Could not find resource: ", //$NON-NLS-1$
158:                                resource, " in ", getBundleHeader( //$NON-NLS-1$
159:                                        bundle, Constants.BUNDLE_NAME))
160:                                .toString();
161:                        Log.warning(msg);
162:                        return resource;
163:                    }
164:                    /*
165:                    localLocation = FileLocator.toFileURL(localLocation);
166:                    return localLocation.toExternalForm();
167:                     */
168:                    return toExternalForm(localLocation);
169:                } catch (Exception e) {
170:                    String msg = StringUtil.concat("Failed to load resource: ", //$NON-NLS-1$
171:                            resource, " from ", getBundleHeader(bundle, //$NON-NLS-1$
172:                                    Constants.BUNDLE_NAME)).toString();
173:                    Log.error(msg, e);
174:                    return resource;
175:                }
176:            }
177:
178:            private static String toExternalForm(URL localURL) {
179:                try {
180:                    localURL = FileLocator.toFileURL(localURL);
181:                    String result = localURL.toExternalForm();
182:                    if (result.startsWith("file:/")) { //$NON-NLS-1$
183:                        if (result.startsWith("file:///") == false) { //$NON-NLS-1$
184:                            result = "file:///" + result.substring(6); //$NON-NLS-1$
185:                        }
186:                    }
187:                    return result;
188:                } catch (IOException e) {
189:                    String msg = "Failed to resolve URL: " //$NON-NLS-1$
190:                            + localURL.toString();
191:                    Log.error(msg, e);
192:                    return localURL.toString();
193:                }
194:            }
195:
196:            /** *** used by Intro parser ***** */
197:            /*
198:             * Util method to return an URL to a plugin relative resource.
199:             */
200:            public static URL getResourceAsURL(String resource, String pluginId) {
201:                Bundle bundle = Platform.getBundle(pluginId);
202:                URL localLocation = FileLocator.find(bundle,
203:                        new Path(resource), null);
204:                return localLocation;
205:            }
206:
207:            /** ********************* Used by HTML generator ****************** */
208:            /**
209:             * Get the absolute path of the given bundle, in the form
210:             * file:/path_to_plugin
211:             * 
212:             * @param bundle
213:             * @return
214:             */
215:            public static String getResolvedBundleLocation(Bundle bundle) {
216:                try {
217:                    URL bundleLocation = bundle.getEntry(""); //$NON-NLS-1$
218:                    if (bundleLocation == null)
219:                        return null;
220:                    /*
221:                    bundleLocation = FileLocator.toFileURL(bundleLocation);
222:                    return bundleLocation.toExternalForm();
223:                     */
224:                    return toExternalForm(bundleLocation);
225:                } catch (IllegalStateException e) {
226:                    Log.error("Failed to access bundle: " //$NON-NLS-1$
227:                            + bundle.getSymbolicName(), e);
228:                    return null;
229:                } /* catch (IOException e) {
230:                           Log.error("Failed to resolve URL path for bundle: " //$NON-NLS-1$
231:                                   + bundle.getSymbolicName(), e);
232:                           return null;
233:                       } */
234:            }
235:
236:            /**
237:             * Get the absolute path of the bundle with id <code>bundleId</code>. If
238:             * no such bundle is found, return null.
239:             * 
240:             * @param bundleId
241:             * @return
242:             */
243:            public static String getResolvedBundleLocation(String bundleId) {
244:                Bundle bundle = Platform.getBundle(bundleId);
245:                if (bundle == null)
246:                    return null;
247:                return getResolvedBundleLocation(bundle);
248:            }
249:
250:        }
w_w_w___.j_a__v___a__2___s.___c_o_m_ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.