KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > internal > base > remote > RemoteContextProvider


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.help.internal.base.remote;
12
13 import java.io.IOException JavaDoc;
14 import java.io.InputStream JavaDoc;
15 import java.net.HttpURLConnection JavaDoc;
16 import java.net.URL JavaDoc;
17
18 import org.eclipse.help.AbstractContextProvider;
19 import org.eclipse.help.IContext;
20 import org.eclipse.help.internal.base.HelpBasePlugin;
21 import org.eclipse.help.internal.context.Context;
22 import org.eclipse.help.internal.dynamic.DocumentReader;
23
24 /*
25  * Provides the context-sensitive help data that is located on the remote
26  * infocenter for a particular id, if the system is configured for remote help.
27  * If not, returns null.
28  */

29 public class RemoteContextProvider extends AbstractContextProvider {
30
31     private static final String JavaDoc PATH_CONTEXT = "/context"; //$NON-NLS-1$
32
private static final String JavaDoc PARAM_ID = "id"; //$NON-NLS-1$
33
private static final String JavaDoc PARAM_LANG = "lang"; //$NON-NLS-1$
34

35     private DocumentReader reader;
36     
37     public IContext getContext(String JavaDoc id, String JavaDoc locale) {
38         if (RemoteHelp.isEnabled()) {
39             InputStream JavaDoc in = null;
40             try {
41                 URL JavaDoc url = RemoteHelp.getURL(PATH_CONTEXT + '?' + PARAM_ID + '=' + id + '&' + PARAM_LANG + '=' + locale);
42                 HttpURLConnection JavaDoc connection = (HttpURLConnection JavaDoc)url.openConnection();
43                 if (connection.getResponseCode() == 200) {
44                     in = connection.getInputStream();
45                     if (reader == null) {
46                         reader = new DocumentReader();
47                     }
48                     return (Context)reader.read(in);
49                 }
50             }
51             catch (IOException JavaDoc e) {
52                 String JavaDoc msg = "I/O error while trying to contact the remote help server"; //$NON-NLS-1$
53
HelpBasePlugin.logError(msg, e);
54             }
55             catch (Throwable JavaDoc t) {
56                 String JavaDoc msg = "Internal error while reading context-sensitive help data from remote server"; //$NON-NLS-1$
57
HelpBasePlugin.logError(msg, t);
58             }
59             finally {
60                 if (in != null) {
61                     try {
62                         in.close();
63                     }
64                     catch (IOException JavaDoc e) {
65                         // nothing more we can do
66
}
67                 }
68             }
69         }
70         return null;
71     }
72     
73     /* (non-Javadoc)
74      * @see org.eclipse.help.AbstractContextProvider#getPlugins()
75      */

76     public String JavaDoc[] getPlugins() {
77         // this is a global provider
78
return null;
79     }
80 }
81
Popular Tags