Skip to main content
summaryrefslogtreecommitdiffstats
blob: e9dc26037ae140a90501d21f19360321c9586037 (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
/***************************************************************************************************
 * 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.web.deployables;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.util.EList;
import org.eclipse.jst.j2ee.internal.deployables.J2EEDeployableFactory;
import org.eclipse.jst.j2ee.internal.project.IWebNatureConstants;
import org.eclipse.jst.j2ee.internal.project.J2EENature;
import org.eclipse.wst.common.modulecore.ModuleCoreNature;
import org.eclipse.wst.common.modulecore.ModuleStructuralModel;
import org.eclipse.wst.common.modulecore.ProjectComponents;
import org.eclipse.wst.common.modulecore.WorkbenchComponent;
import org.eclipse.wst.server.core.IModule;
import org.eclipse.wst.server.core.internal.Module;
import org.eclipse.wst.server.core.model.ModuleDelegate;

import org.eclipse.jem.util.logger.proxy.Logger;

public class WebDeployableFactory extends J2EEDeployableFactory {
	private static final String ID = "com.ibm.wtp.web.server"; //$NON-NLS-1$

	protected static final IPath[] PATHS = new IPath[]{new Path(".j2ee") //$NON-NLS-1$
	};

	public String getFactoryId() {
		return ID;
	}

	public String getNatureID() {
		return IWebNatureConstants.J2EE_NATURE_ID;
	}

	public IModule createModule(J2EENature nature) {
		return null;
	}

	protected IPath[] getListenerPaths() {
		return PATHS;
	}

	protected List createModules(ModuleCoreNature nature) {
		IProject project = nature.getProject();
		List modules = new ArrayList(1);
		ModuleStructuralModel moduleStructureModel = null;
		try {
			ModuleCoreNature moduleCoreNature = ModuleCoreNature.getModuleCoreNature(project);
			moduleStructureModel = moduleCoreNature.getModuleStructuralModelForRead(this);
			ProjectComponents components = (ProjectComponents) moduleStructureModel.getPrimaryRootObject();
			EList workBenchModules = components.getComponents();
			if (workBenchModules.isEmpty())
				return modules;
			modules = createModuleDelegates(workBenchModules, project);

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			moduleStructureModel.releaseAccess(this);
		}
		return modules;
	}

	private List createModuleDelegates(EList workBenchModules, IProject project) throws CoreException {
		J2EEFlexProjWebDeployable moduleDelegate = null;
		IModule module = null;
		List moduleList = new ArrayList(workBenchModules.size());
		//  J2EENature nature = (J2EENature)project.getNature(getNatureID());

		for (int i = 0; i < workBenchModules.size(); i++) {
			try {
				WorkbenchComponent wbModule = (WorkbenchComponent) workBenchModules.get(i);
				moduleDelegate = new J2EEFlexProjWebDeployable(project, ID, wbModule);
				module = createModule(wbModule.getName(), wbModule.getName(), moduleDelegate.getType(), moduleDelegate.getVersion(), moduleDelegate.getProject());
				moduleList.add(module);
				moduleDelegate.initialize(module);
				//adapt(moduleDelegate, (WorkbenchComponent) workBenchModules.get(i));
			} catch (Exception e) {
				Logger.getLogger().write(e);
			} finally {
				if (module != null) {
					if (getModuleDelegate(module) == null)
						moduleDelegates.add(moduleDelegate);
				}
			}
		}
		return moduleList;

	}

	private void adapt(J2EEFlexProjWebDeployable moduleDelegate, WorkbenchComponent wbModule) {

		ModuleAdapter moduleAdapter = new ModuleAdapter() {
			public void notifyChanged(Notification msg) {
				super.notifyChanged(msg);
			}
		};
		moduleAdapter.setTarget(wbModule);
		moduleAdapter.setModuleDelegate(moduleDelegate);
		wbModule.eAdapters().add(moduleAdapter);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.wst.server.core.model.ModuleFactoryDelegate#getModules()
	 */
	public IModule[] getModules() {
		int i = 0;
		cacheModules();
		ArrayList moduleList = new ArrayList();
		for (Iterator iter = projects.values().iterator(); iter.hasNext();) {
			IModule[] element = (IModule[]) iter.next();
			for (int j = 0; j < element.length; j++) {
				moduleList.add((IModule) element[j]);
			}

		}
		IModule[] modules = new IModule[moduleList.size()];
		moduleList.toArray(modules);
		return modules;

	}
}

Back to the top