Skip to main content
summaryrefslogtreecommitdiffstats
blob: dcc43507b7c4194d8b48105430fa4083d0c91953 (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
/*******************************************************************************
 * Copyright (c) 2003, 2006 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
 * yyyymmdd bug      Email and other contact information
 * -------- -------- -----------------------------------------------------------
 * 20060524   130755 kathy@ca.ibm.com - Kathy Chan
 *******************************************************************************/
package org.eclipse.jst.ws.internal.axis.creation.ui.command;


import java.io.FileInputStream;
import java.io.IOException;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;
import org.eclipse.jem.util.emf.workbench.ProjectUtilities;
import org.eclipse.jst.ws.internal.axis.consumption.core.AxisConsumptionCoreMessages;
import org.eclipse.jst.ws.internal.common.J2EEUtils;
import org.eclipse.jst.ws.internal.plugin.WebServicePlugin;
import org.eclipse.osgi.util.NLS;
import org.eclipse.wst.command.internal.env.common.FileResourceUtils;
import org.eclipse.wst.command.internal.env.core.common.StatusUtils;
import org.eclipse.wst.command.internal.env.core.context.ResourceContext;
import org.eclipse.wst.common.componentcore.resources.IVirtualComponent;
import org.eclipse.wst.common.componentcore.resources.IVirtualFile;
import org.eclipse.wst.common.componentcore.resources.IVirtualFolder;
import org.eclipse.wst.common.environment.IEnvironment;
import org.eclipse.wst.common.environment.IStatusHandler;
import org.eclipse.wst.common.frameworks.datamodel.AbstractDataModelOperation;
import org.eclipse.wst.server.core.IModule;
import org.eclipse.wst.server.core.IServer;
import org.eclipse.wst.server.core.ServerCore;
import org.eclipse.wst.server.core.ServerUtil;
import org.eclipse.wst.server.core.internal.IModulePublishHelper;

/**
 * 
 * This command copies the server-config.wsdd file to it's proper location in the module.
 * 
 */

public class CopyDeploymentFileCommand extends AbstractDataModelOperation
{
	private final String WEB_INF = "WEB-INF";
	private final String SERVER_CONFIG = "server-config.wsdd";
    private String projectName_;
    private String EARProjectName_;
	private String serverInstanceId_;
  
  /**
   * Constructor for CopyDeploymentFileCommand.
 * @param String description
 * @param String name
   * 
   */
  public CopyDeploymentFileCommand( String projectName, String earProjectName )
  { 
    projectName_   = projectName;
    EARProjectName_   = earProjectName;
  }

  public IStatus execute( IProgressMonitor monitor, IAdaptable adaptable ) 
  {
	  IStatus status = Status.OK_STATUS;
	  IEnvironment environment = getEnvironment();
	  
	  FileInputStream finStream = null;
	  try
	  {
		  
		  IVirtualComponent component = J2EEUtils.getVirtualComponent( projectName_ );
		  IServer server = ServerCore.findServer( serverInstanceId_ );
		  IProject project = ProjectUtilities.getProject(projectName_);
		  IModule projectModule = ServerUtil.getModule(project);
		  
		  if (server != null && component != null && projectModule != null) {
			  IModulePublishHelper publishHelper = (IModulePublishHelper) 
			  	server.loadAdapter(IModulePublishHelper.class, monitor);
			  if (publishHelper != null) {
				  IModule[] serverModules;
				  if (EARProjectName_ == null) {
					  serverModules = new IModule [] {projectModule};
				  } else {
					  IProject EARProject = ProjectUtilities.getProject(EARProjectName_);
					  IModule EARProjectModule = ServerUtil.getModule(EARProject);
					  serverModules = new IModule [] {EARProjectModule, projectModule};
				  }
				  IPath publishDirPath = publishHelper.getPublishDirectory(serverModules);
				  if (publishDirPath != null) {
					  IPath path = new Path( WEB_INF ).append( SERVER_CONFIG );
					  IPath serverConfigPath = publishDirPath.append(path);
					  if (serverConfigPath != null) {
						  IVirtualFolder rootFolder = component.getRootFolder();
						  if (rootFolder != null) {			  
							  IVirtualFile newLocation = rootFolder.getFile(path);
							  IPath targetPath = newLocation.getWorkspaceRelativePath();
							  
							  if (targetPath != null) {
								  finStream = new FileInputStream(serverConfigPath.toString());
								  if (finStream != null) {
									  IStatusHandler statusHandler = environment.getStatusHandler();
									  ResourceContext context = WebServicePlugin.getInstance().getResourceContext();
									  FileResourceUtils.createFile(context,  
											  targetPath,
											  finStream,
											  monitor,
											  statusHandler);
									  finStream.close();
								  }
							  }
						  }
					  }
				  }
			  } 	  		  
		  }	  
	  }
	  catch( Throwable e )
	  {
		  status = StatusUtils.errorStatus(NLS.bind(AxisConsumptionCoreMessages.MSG_ERROR_MOVE_RESOURCE, new String[]{e.getLocalizedMessage()}), e);
		  environment.getStatusHandler().reportError(status);
	  } finally {
		  if (finStream != null) {
			  try {
				  finStream.close();
			  } catch (IOException e) {
			  }
		  }			
	  }
	  
	  return status;
	  
  }

	public void setServerInstanceId(String serverInstanceId) {
		this.serverInstanceId_ = serverInstanceId;
	}
}

Back to the top