Source Code Cross Referenced for BundleUtil.java in  » IDE-Eclipse » ui » org » eclipse » ui » internal » intro » impl » model » 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.impl.model.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.impl.model.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.IProduct;
019:        import org.eclipse.core.runtime.Path;
020:        import org.eclipse.core.runtime.Platform;
021:        import org.eclipse.ui.internal.intro.impl.util.Log;
022:        import org.eclipse.ui.internal.intro.impl.util.StringUtil;
023:        import org.osgi.framework.Bundle;
024:        import org.osgi.framework.Constants;
025:
026:        /**
027:         * Bundle convenience methods.
028:         */
029:        public class BundleUtil {
030:
031:            private static String NL_TAG = "$nl$/"; //$NON-NLS-1$
032:            private final static String PRODUCT_PLUGIN = "PRODUCT_PLUGIN"; //$NON-NLS-1$
033:            private final static String PLUGINS_ROOT = "PLUGINS_ROOT/"; //$NON-NLS-1$
034:
035:            /**
036:             * Utility method to validate the state of a bundle. Log invalid bundles to
037:             * log file.
038:             */
039:            public static boolean bundleHasValidState(Bundle bundle) {
040:                if (bundle == null || bundle.getState() == Bundle.UNINSTALLED
041:                        || bundle.getState() == Bundle.INSTALLED) {
042:
043:                    if (bundle == null)
044:                        Log.error("Intro tried accessing a NULL bundle.", null); //$NON-NLS-1$
045:                    else {
046:                        String msg = StringUtil
047:                                .concat(
048:                                        "Intro tried accessing Bundle: ", getBundleHeader( //$NON-NLS-1$
049:                                                bundle, Constants.BUNDLE_NAME),
050:                                        " vendor: ", //$NON-NLS-1$
051:                                        getBundleHeader(bundle,
052:                                                Constants.BUNDLE_VENDOR),
053:                                        " bundle state: ", String.valueOf(bundle.getState())).toString(); //$NON-NLS-1$
054:                        Log.error(msg, null);
055:                    }
056:                    return false;
057:                }
058:
059:                return true;
060:            }
061:
062:            /**
063:             * Retrieves the given key from the bundle header.
064:             * 
065:             * @param bundle
066:             * @param key
067:             * @return
068:             */
069:            public static String getBundleHeader(Bundle bundle, String key) {
070:                return (String) bundle.getHeaders().get(key);
071:            }
072:
073:            public static Bundle getBundleFromConfigurationElement(
074:                    IConfigurationElement cfg) {
075:                return Platform.getBundle(cfg.getContributor().getName());
076:            }
077:
078:            /**
079:             * Get the resourcelocation, but do not force an $nl$ on it.
080:             * 
081:             * @param resource
082:             * @param element
083:             * @return
084:             */
085:            public static String getResourceLocation(String resource,
086:                    IConfigurationElement element) {
087:                Bundle bundle = getBundleFromConfigurationElement(element);
088:                return getResolvedResourceLocation(resource, bundle, false);
089:            }
090:
091:            /**
092:             * Returns the fully qualified location of the passed resource string from
093:             * the passed plugin id. If the file could not be loaded from the plugin,
094:             * the resource is returned as is.
095:             * 
096:             * @param resource
097:             * @return
098:             */
099:            public static String getResolvedResourceLocation(String resource,
100:                    String pluginId) {
101:                Bundle bundle = Platform.getBundle(pluginId);
102:                return getResolvedResourceLocation(resource, bundle, true);
103:            }
104:
105:            /**
106:             * Shorthand util method.
107:             * 
108:             * @param resource
109:             * @return
110:             */
111:            public static String getResolvedResourceLocation(String resource,
112:                    Bundle bundle) {
113:                return getResolvedResourceLocation(resource, bundle, true);
114:            }
115:
116:            public static String getResolvedResourceLocation(String base,
117:                    String resource, Bundle bundle) {
118:                // quick exits.
119:                if (resource == null)
120:                    return null;
121:
122:                String fullResource = new Path(base).append(resource)
123:                        .toString();
124:                String resolvedResource = getResolvedResourceLocation(
125:                        fullResource, bundle, true);
126:
127:                if (resolvedResource.equals(fullResource))
128:                    // return resource as is when the resource does not exist.
129:                    return resource;
130:                return resolvedResource;
131:            }
132:
133:            public static String getResolvedResourceLocation(String resource,
134:                    Bundle bundle, boolean forceNLResolve) {
135:                // quick exits.
136:                if (resource == null)
137:                    return null;
138:
139:                if (bundle == null || !bundleHasValidState(bundle))
140:                    return resource;
141:
142:                URL localLocation = null;
143:                try {
144:                    // resolve PLUGINS_ROOT
145:                    int index = resource.indexOf(PLUGINS_ROOT);
146:                    if (index != -1) {
147:                        resource = resource.substring(index
148:                                + PLUGINS_ROOT.length());
149:                        index = resource.indexOf('/');
150:                        if (index != -1) {
151:                            String bundleName = resource.substring(0, index);
152:                            if (PRODUCT_PLUGIN.equals(bundleName)) {
153:                                IProduct product = Platform.getProduct();
154:                                if (product != null) {
155:                                    Bundle productBundle = product
156:                                            .getDefiningBundle();
157:                                    if (productBundle != null) {
158:                                        bundleName = productBundle
159:                                                .getSymbolicName();
160:                                    }
161:                                }
162:                            }
163:                            resource = resource.substring(index + 1);
164:                            Bundle actualBundle = Platform
165:                                    .getBundle(bundleName);
166:                            if (actualBundle != null) {
167:                                return getResolvedResourceLocation(resource,
168:                                        actualBundle, forceNLResolve);
169:                            }
170:                        }
171:                    }
172:
173:                    // we need to resolve this URL.
174:                    String copyResource = resource;
175:                    if (forceNLResolve && !copyResource.startsWith(NL_TAG)) {
176:                        if (copyResource.startsWith("/") //$NON-NLS-1$
177:                                || copyResource.startsWith("\\")) //$NON-NLS-1$
178:                            copyResource = resource.substring(1);
179:                        copyResource = NL_TAG + copyResource;
180:                    }
181:                    IPath resourcePath = new Path(copyResource);
182:                    localLocation = FileLocator
183:                            .find(bundle, resourcePath, null);
184:                    if (localLocation == null) {
185:                        // localLocation can be null if the passed resource could not
186:                        // be found relative to the plugin. log fact, return resource,
187:                        // as is.
188:                        String msg = StringUtil.concat(
189:                                "Could not find resource: ", //$NON-NLS-1$
190:                                resource, " in ", getBundleHeader( //$NON-NLS-1$
191:                                        bundle, Constants.BUNDLE_NAME))
192:                                .toString();
193:                        Log.warning(msg);
194:                        return resource;
195:                    }
196:                    /*
197:                    localLocation = FileLocator.toFileURL(localLocation);
198:                    return localLocation.toExternalForm();
199:                     */
200:                    return toExternalForm(localLocation);
201:                } catch (Exception e) {
202:                    String msg = StringUtil.concat("Failed to load resource: ", //$NON-NLS-1$
203:                            resource, " from ", getBundleHeader(bundle, //$NON-NLS-1$
204:                                    Constants.BUNDLE_NAME)).toString();
205:                    Log.error(msg, e);
206:                    return resource;
207:                }
208:            }
209:
210:            /** *** used by Intro parser ***** */
211:            /*
212:             * Util method to return an URL to a plugin relative resource.
213:             */
214:            public static URL getResourceAsURL(String resource, String pluginId) {
215:                Bundle bundle = Platform.getBundle(pluginId);
216:                URL localLocation = FileLocator.find(bundle,
217:                        new Path(resource), null);
218:                return localLocation;
219:            }
220:
221:            /** ********************* Used by HTML generator ****************** */
222:            /**
223:             * Get the absolute path of the given bundle, in the form
224:             * file:/path_to_plugin
225:             * 
226:             * @param bundle
227:             * @return
228:             */
229:            public static String getResolvedBundleLocation(Bundle bundle) {
230:                try {
231:                    URL bundleLocation = bundle.getEntry(""); //$NON-NLS-1$
232:                    if (bundleLocation == null)
233:                        return null;
234:                    /*
235:                    bundleLocation = FileLocator.toFileURL(bundleLocation);
236:                    return bundleLocation.toExternalForm();
237:                     */
238:                    return toExternalForm(bundleLocation);
239:                } catch (IllegalStateException e) {
240:                    Log.error("Failed to access bundle: " //$NON-NLS-1$
241:                            + bundle.getSymbolicName(), e);
242:                    return null;
243:                }/* catch (IOException e) {
244:                            Log.error("Failed to resolve URL path for bundle: " //$NON-NLS-1$
245:                                    + bundle.getSymbolicName(), e);
246:                            return null;
247:                        } */
248:            }
249:
250:            /**
251:             * Get the absolute path of the bundle with id <code>bundleId</code>. If
252:             * no such bundle is found, return null.
253:             * 
254:             * @param bundleId
255:             * @return
256:             */
257:            public static String getResolvedBundleLocation(String bundleId) {
258:                Bundle bundle = Platform.getBundle(bundleId);
259:                if (bundle == null)
260:                    return null;
261:                return getResolvedBundleLocation(bundle);
262:            }
263:
264:            /*
265:             * Bug 126085 - need to fix up file: protocol
266:             * to a form that IE7 understands (file:///). 
267:             */
268:
269:            private static String toExternalForm(URL localURL) {
270:                try {
271:                    localURL = FileLocator.toFileURL(localURL);
272:                    String result = localURL.toExternalForm();
273:                    if (result.startsWith("file:/")) { //$NON-NLS-1$
274:                        if (result.startsWith("file:///") == false) { //$NON-NLS-1$
275:                            result = "file:///" + result.substring(6); //$NON-NLS-1$
276:                        }
277:                    }
278:                    return result;
279:                } catch (IOException e) {
280:                    String msg = "Failed to resolve URL: " //$NON-NLS-1$
281:                            + localURL.toString();
282:                    Log.error(msg, e);
283:                    return localURL.toString();
284:                }
285:            }
286:
287:        }
w___ww__.j__a___va___2s___._com__ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.