Skip to main content
summaryrefslogtreecommitdiffstats
blob: 5003dc6214275a31c5844c78d189a7ce3e475791 (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
/*******************************************************************************
 * Copyright (c) 2003, 2004 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
 *******************************************************************************/
/*
 * Created on Sep 12, 2003
 */
package org.eclipse.jst.j2ee.internal.web.operations;

import java.util.List;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.jst.j2ee.internal.plugin.J2EEPlugin;
import org.eclipse.jst.j2ee.internal.servertarget.IServerTargetConstants;
import org.eclipse.wst.server.core.IRuntime;
import org.eclipse.wst.server.core.ServerCore;

/**
 * A class with some useful methods that support server targetting for Web projects.
 * 
 * @author Pratik Shah
 */
public class ServerTargetUtil {

	private static final String defaultId = "com.ibm.etools.websphere.serverTarget.base.v51"; //$NON-NLS-1$
	private static final String defaultExpressId = "com.ibm.etools.websphere.serverTarget.express.v51"; //$NON-NLS-1$

	/**
	 * @param targets
	 *            A list of IServerTargets
	 * @return The index of the target server with the
	 *         {@link #getDefaultServerTargetId() default Id}; or 0, if it could not be found.
	 */
	public static int findDefaultServerTargetIndex(List targets) {
		int index = 0;
		for (int i = 0; i < targets.size(); i++) {
			IRuntime target = (IRuntime) targets.get(i);
			if (target.getId().equals(getDefaultServerTargetId())) {
				index = i;
				break;
			}
		}
		return index;
	}

	/**
	 * @param target
	 *            The IServerTarget that has to be found in the given list; it can be
	 *            <code>null</code>
	 * @param list
	 *            The List from which the IServerTarget has to be found
	 * 
	 * @return The index of the given target in the given list; or 0, if the given target could not
	 *         be found in the given list. Two IServerTargets are considered to be equal if they
	 *         have the same ID.
	 */
	public static int findIndexOf(IRuntime target, List list) {
		int index = 0;
		if (target != null) {
			for (int i = 0; i < list.size(); i++) {
				IRuntime element = (IRuntime) list.get(i);
				if (element.equals(target)) {
					index = i;
					break;
				}
			}
		}
		return index;
	}

	/**
	 * @return the Id of the target server that should be selected by default
	 */
	public static String getDefaultServerTargetId() {
		String id = defaultExpressId;
		if (J2EEPlugin.isEJBSupportAvailable()) {
			id = defaultId;
		}
		return id;
	}

	/**
	 * @param isJ2EE13
	 *            <code>true</code> if the constant for J2EE version 1.3 is desired
	 * @return IServerTargetConstants.J2EE_12 or IServerTargetConstants.J2EE_13
	 */
	public static String getJ2EEVersion(boolean isJ2EE13) {
		return isJ2EE13 ? IServerTargetConstants.J2EE_13 : IServerTargetConstants.J2EE_12;
	}

	/**
	 * A convenient method that takes in a list of IServerTargets and returns an array of labels of
	 * the IServerTargets in the given list.
	 * 
	 * @param serverTargets
	 *            The list of IServerTargets
	 * @return An array lof labels of the IServerTargets in the given list
	 */
	public static String[] getServerNames(List serverTargets) {
		String[] result = new String[serverTargets.size()];
		for (int i = 0; i < result.length; i++) {
			IRuntime runtime = (IRuntime) serverTargets.get(i);
			result[i] = runtime.getName() + " (" + runtime.getRuntimeType().getName() + ")"; //$NON-NLS-1$ //$NON-NLS-2$
		}
		return result;
	}

	/**
	 * @param prjName
	 *            name of the project whose ServerTarget has to be retrieved; it can be
	 *            <code>null</code>
	 * 
	 * @return the ServerTarget of the given project; <code>null</code> if the project does not
	 *         exist or its ServerTarget is not specified.
	 */
	public static IRuntime getServerTarget(String prjName) {
		if (prjName != null && !prjName.trim().equals("")) { //$NON-NLS-1$
			IProject prj = ResourcesPlugin.getWorkspace().getRoot().getProject(prjName);
			if (prj != null && prj.exists()) {
				return ServerCore.getProjectProperties(prj).getRuntimeTarget();
			}
		}
		return null;
	}

}

Back to the top