Skip to main content
summaryrefslogtreecommitdiffstats
blob: c502cbdf3b8dc079ef5f1ba20a77e5be6265f451 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*******************************************************************************
 * Copyright (c) 2009, 2011 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.help.internal.base.remote;

import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.Hashtable;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.help.internal.base.HelpBasePlugin;
import org.eclipse.help.internal.base.IHelpBaseConstants;
import org.eclipse.help.internal.base.util.TestConnectionUtility;

public class RemoteStatusData {

	private static int TIMEOUT = 60 * 1000;


	/*
	 * Convenience method to see if any remote help
	 * is down
	 */
	public static boolean isAnyRemoteHelpUnavailable()
	{
		ArrayList<URL> sites = getRemoteSites();
		if (sites.isEmpty())
			return false;

		for (int s=0;s<sites.size();s++)
			if (!isConnected(sites.get(s)))
				return true;

		return false;
	}

	/*
	 * Checks each URL in the ArrayList site to see if
	 * a network connection can be opened to
	 * url+/index.jsp
	 *
	 * Returns a subset of sites that cannot be connected.
	 * May be empty, or may be the same as sites
	 */
	public static ArrayList<URL> checkSitesConnectivity(ArrayList<URL> sites)
	{
		ArrayList<URL> badSites = new ArrayList<URL>();

		for (int i=0;i<sites.size();i++)
			if (!isConnected(sites.get(i)))
				badSites.add(sites.get(i));

		return badSites;
	}

	public static boolean isConnected(URL site)
	{
		ConnectionCache cache = ConnectionCache.getCache();
		try{
			return cache.isConnected(site);
		}catch(CoreException e)
		{
			boolean connected = TestConnectionUtility.testConnection(site.getHost(),
					"" + site.getPort(), site.getPath(), site.getProtocol()); //$NON-NLS-1$
			cache.put(site, connected);
			cache.resetTimer();
			return connected;
		}
	}

	/*
	 * Loads the remote sites stored in preferences,
	 * and places them as URLs in an ArrayList.
	 *
	 * Returns the ArrayList with sites in URL form
	 */
	public static ArrayList<URL> getRemoteSites()
	{
		ArrayList<URL> sites = new ArrayList<URL>();

		boolean remoteHelpEnabled =
			Platform.getPreferencesService().getBoolean(
					HelpBasePlugin.PLUGIN_ID, IHelpBaseConstants.P_KEY_REMOTE_HELP_ON, false,null);

		if (!remoteHelpEnabled)
			return sites;

		String hosts[] = Platform.getPreferencesService().getString(
				HelpBasePlugin.PLUGIN_ID, IHelpBaseConstants.P_KEY_REMOTE_HELP_HOST, "", null).split(","); //$NON-NLS-1$ //$NON-NLS-2$
		String paths[] = Platform.getPreferencesService().getString(
				HelpBasePlugin.PLUGIN_ID, IHelpBaseConstants.P_KEY_REMOTE_HELP_PATH, "", null).split(","); //$NON-NLS-1$ //$NON-NLS-2$
		String protocols[] = Platform.getPreferencesService().getString(
				HelpBasePlugin.PLUGIN_ID, IHelpBaseConstants.P_KEY_REMOTE_HELP_PROTOCOL, "", null).split(","); //$NON-NLS-1$ //$NON-NLS-2$
		String ports[] = Platform.getPreferencesService().getString(
				HelpBasePlugin.PLUGIN_ID, IHelpBaseConstants.P_KEY_REMOTE_HELP_PORT, "", null).split(","); //$NON-NLS-1$ //$NON-NLS-2$
		String enableds[] = Platform.getPreferencesService().getString(
				HelpBasePlugin.PLUGIN_ID, IHelpBaseConstants.P_KEY_REMOTE_HELP_ICEnabled, "", null).split(","); //$NON-NLS-1$ //$NON-NLS-2$


		for (int i=0;i<hosts.length;i++)
		{
			try{
				if (enableds[i].equalsIgnoreCase("true")) //$NON-NLS-1$
				{
					URL url = new URL(
							protocols[i]+"://"+hosts[i]+':'+ports[i]+paths[i]); //$NON-NLS-1$
					sites.add(url);
				}
			}
			catch(Exception ex){
			}
		}
		return sites;
	}

	public static void clearCache() {
		ConnectionCache.clear();
	}

	private static class ConnectionCache
	{
		private static ConnectionCache instance;

		private Hashtable<URL, Boolean> cache;
		private long start;

		private ConnectionCache(){

			cache = new Hashtable<URL, Boolean>();
			resetTimer();
		}

		public void resetTimer()
		{
			start = new Date().getTime();
		}

		public static ConnectionCache getCache()
		{
			if (instance==null || instance.isExpired())
			{
				instance = new ConnectionCache();
			}
			return instance;
		}

		public static void clear()
		{
			instance = null;
		}

		public boolean isExpired()
		{
			long now = new Date().getTime();

			return (now > start + TIMEOUT);
		}

		public boolean isConnected(URL url) throws CoreException
		{
			Boolean b = cache.get(url);
			if (b==null)
				throw new CoreException(new Status(IStatus.ERROR,HelpBasePlugin.PLUGIN_ID,"Cache Unavailable")); //$NON-NLS-1$

			return b.booleanValue();
		}

		public void put(URL url,boolean connected)
		{
			cache.put(url,new Boolean(connected));
		}
	}

}

Back to the top