Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

summaryrefslogtreecommitdiffstats
blob: cabb69390afe2317a0cd53f9052251c137f79adb (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
/*******************************************************************************
 * 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
 *******************************************************************************/
package org.eclipse.jst.j2ee.internal.deployables;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jem.util.logger.proxy.Logger;
import org.eclipse.wst.common.componentcore.ComponentCore;
import org.eclipse.wst.common.componentcore.ModuleCoreNature;
import org.eclipse.wst.common.componentcore.internal.StructureEdit;
import org.eclipse.wst.common.componentcore.internal.util.IModuleConstants;
import org.eclipse.wst.common.componentcore.resources.IFlexibleProject;
import org.eclipse.wst.common.componentcore.resources.IVirtualComponent;
import org.eclipse.wst.server.core.IModule;
import org.eclipse.wst.server.core.model.ModuleDelegate;
import org.eclipse.wst.server.core.util.ProjectModuleFactoryDelegate;

/**
 * J2EE deployable factory superclass.
 */
public abstract class J2EEDeployableFactory extends ProjectModuleFactoryDelegate {

	protected HashMap projectModules;

	protected final List moduleDelegates = Collections.synchronizedList(new ArrayList(1));

	private long cachedStamp = -2;

	protected static boolean isFlexibleProject(IProject project) {
		return ModuleCoreNature.getModuleCoreNature(project) != null;
	}

	public J2EEDeployableFactory() {
		super();
	}



	protected boolean needsUpdating(IProject project) {
		if(!initialized)
			return true;
		IFile wtpmodules = project.getFile(IModuleConstants.WTPMODULE_FILE_PATH);
		if (!wtpmodules.exists())
			return false;
		if(wtpmodules.getModificationStamp() != cachedStamp) {
			cachedStamp = wtpmodules.getModificationStamp();
			return true;
		}
		return false;
	}


	/**
	 * Return the workspace root.
	 * 
	 * @return the workspace root
	 */
	private static IWorkspaceRoot getWorkspaceRoot() {
		return ResourcesPlugin.getWorkspace().getRoot();
	}



	protected IModule[] createModules(IProject project) {

		// List modules = createModules(nature);
		ModuleCoreNature nature = null;
		try {
			nature = (ModuleCoreNature) project.getNature(IModuleConstants.MODULE_NATURE_ID);
		} catch (CoreException e) {
			Logger.getLogger().write(e);
		}
		List modules = createModules(nature);
		if (modules == null)
			return new IModule[0];
		IModule[] moduleArray = new IModule[modules.size()];
		modules.toArray(moduleArray);
		return moduleArray;
	}
	
	protected List createModules(ModuleCoreNature nature) {
		IProject project = nature.getProject();
		List modules = new ArrayList(1); 
		StructureEdit moduleCore = null;
		try {
			IFlexibleProject flexProj = ComponentCore.createFlexibleProject(project);
			IVirtualComponent[] components = flexProj.getComponents();
			modules = createModuleDelegates(components);

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if(moduleCore != null) 
				moduleCore.dispose();
		}
		return modules;
	}


	protected abstract List createModuleDelegates(IVirtualComponent[] comp) throws CoreException; 

	
	protected boolean isValidModule(IProject project) {
		try {
			return isFlexibleProject(project);
		} catch (Exception e) {
			//Ignore
		}
		return false;
	}

	protected abstract String getNatureID();

	
/*	protected IModule createModule(IProject project) {
		try {
			J2EENature nature = (J2EENature) project.getNature(getNatureID());
			return createModule(nature);
		} catch (Exception e) {
		}
		return null;
	}*/


	public ModuleDelegate getModuleDelegate(IModule module) {
		if(moduleDelegates.size() == 0)
			return null;
		ModuleDelegate[] delegates = (ModuleDelegate[])
			moduleDelegates.toArray(new ModuleDelegate[moduleDelegates.size()]);
		for (int i=0; i<delegates.length; i++) {			
			if (module == delegates[i].getModule())
				return delegates[i];
		}
		return null;
	}



}

Back to the top