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: e4abdc6a8751125df53a90374694541b4b363008 (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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*******************************************************************************
 * Copyright (c) 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
 * yyyymmdd bug      Email and other contact information
 * -------- -------- -----------------------------------------------------------
 * 20060404   134913 sengpl@ca.ibm.com - Seng Phung-Lu      
 * 20060517   142027 sengpl@ca.ibm.com - Seng Phung-Lu
 *******************************************************************************/
package org.eclipse.jst.ws.internal.consumption.ui.command;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.List;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
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.MultiStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jst.ws.internal.common.ResourceUtils;
import org.eclipse.jst.ws.internal.consumption.ui.ConsumptionUIMessages;
import org.eclipse.jst.ws.internal.consumption.ui.common.HandlerDescriptionHolder;
import org.eclipse.jst.ws.internal.consumption.ui.common.HandlerServiceRefHolder;
import org.eclipse.jst.ws.internal.consumption.ui.widgets.object.HandlerTableItem;
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.ui.eclipse.EnvironmentUtils;
import org.eclipse.wst.common.environment.IEnvironment;
import org.eclipse.wst.common.frameworks.datamodel.AbstractDataModelOperation;



public class GenerateHandlerSkeletonCommand extends AbstractDataModelOperation
{
  private IPath outputLocation_;
  private boolean genSkeleton_;
  private String handlerNameForEdit_ = null;
  
  private HandlerServiceRefHolder[] handlerServiceRefHolder_;
  private HandlerDescriptionHolder[]handlerDescriptionHolder_;
  
  public GenerateHandlerSkeletonCommand()
  {
  }
  

  public IStatus execute( IProgressMonitor monitor, IAdaptable adaptable )
  {
  
    IStatus     returnStatus = Status.OK_STATUS;
  	
  	if (!genSkeleton_)	return returnStatus; 
  	
      // form the map of classname to locations
      if (handlerDescriptionHolder_!=null){
        for (int i=0;i<handlerDescriptionHolder_.length;i++){
          Hashtable handlerTable = new Hashtable();
          IPath outPath = handlerDescriptionHolder_[i].getSourceOutputPath();
          List handlers = handlerDescriptionHolder_[i].getHandlerList();
          for (int j=0;j<handlers.size();j++){
            HandlerTableItem hti = (HandlerTableItem)handlers.get(j);
            String className = hti.getHandlerClassName();
            if (className!=null && outPath!=null)
              handlerTable.put(className, outPath);
          }
          returnStatus = genHandlersClasses(handlerTable, monitor);
        }
        
      }
      else {
        for (int i=0;i<handlerServiceRefHolder_.length;i++){
          Hashtable handlerTable = new Hashtable();
          IPath outPath = handlerServiceRefHolder_[i].getSourceOutputPath();
          List handlers = handlerServiceRefHolder_[i].getHandlerList();
          for (int j=0;j<handlers.size();j++){
            HandlerTableItem hti = (HandlerTableItem)handlers.get(j);
            String className = hti.getHandlerClassName();
            if (className!=null && outPath!=null)
              handlerTable.put(className, outPath);
          }
          returnStatus = genHandlersClasses(handlerTable, monitor);
        }
      }
    
 
    return returnStatus;

  }
  
  private IStatus genHandlersClasses(Hashtable handlersForGen, IProgressMonitor monitor){
    IEnvironment env          = getEnvironment();
    MultiStatus status       = null;
    IStatus     returnStatus = Status.OK_STATUS;
    boolean error = false;
    
    IStatus writeStatus;    
    Enumeration keys = handlersForGen.keys();
    while (keys.hasMoreElements()) {
        String className = (String)keys.nextElement();
        writeStatus = writeFile(env, className, (IPath)handlersForGen.get(className), monitor );
        // handle status return
        if (writeStatus.getSeverity() == Status.ERROR) {  // write status is OK or ERROR
            error = true;
            if (status == null) {
                status = StatusUtils.multiStatus( ConsumptionUIMessages.MSG_ERROR_GENERATE_HANDLER_SKELETON, new IStatus[0] );
            }
            status.add(writeStatus);
        } 
    }
    
    if (error) 
    {
       env.getStatusHandler().reportError(status);
       returnStatus = status;
    }
    
    return returnStatus;
    
  }
  
  
  private IStatus writeFile (IEnvironment env, String className, IPath outputLocation, IProgressMonitor monitor ) 
  {
  	IStatus status = Status.OK_STATUS;
  	int index;
  	
  	String simpleClassName = className;
  	String packageName = null;
  	IPath filePath = outputLocation;
  	if (className != null) {
  		index = className.lastIndexOf('.');
  		if (index != -1) {
  			simpleClassName = className.substring(index + 1);
  			packageName = className.substring(0, index);
  			
  		  	String packageSegment = packageName;
  		  	int j = 0;
  		  	j = packageSegment.indexOf('.');
  		  	while (j != -1) {
  		  		filePath = filePath.append(packageSegment.substring(0, j));
  		  		packageSegment = packageSegment.substring(j + 1);
  		  		j = packageSegment.indexOf('.');
  		  	}
  		    filePath = filePath.append(packageSegment);
  		}
  		else {
  		  packageName = "";
  		}
  	}
  	
  	filePath = filePath.append(simpleClassName);
  	
  	filePath = filePath.addFileExtension("java");

  	// check if Handler already exists; do not overwrite existing Handlers
  	IResource workspaceRes = ResourceUtils.findResource(filePath);
  	if (workspaceRes!=null && workspaceRes.exists()) {
  		return status;
  	}
  	
  	if (handlerNameForEdit_ == null){
  		handlerNameForEdit_ = className;
        outputLocation_ = outputLocation;
  	}
  	
  	OutputStream outputStream = FileResourceUtils.newFileOutputStream( EnvironmentUtils.getResourceContext(env), filePath, monitor, env.getStatusHandler());
  	// create buffered writer for writing file
  	BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(outputStream));
  	try {
  		if (packageName.length() != 0) {
  			bw.write("package " +packageName+";");
  			bw.newLine();
  			bw.newLine();
  		}
  		bw.write("import javax.xml.rpc.handler.GenericHandler;");
  		bw.newLine();
  		bw.write("import javax.xml.rpc.handler.MessageContext;");
  		bw.newLine();
  		bw.write("import javax.xml.namespace.QName;");
  		bw.newLine();
  		bw.newLine();
  		
  		bw.write("public class "+simpleClassName+" extends GenericHandler");
  		bw.newLine();
  		bw.write("{");
  		bw.newLine();
  		bw.newLine();
  		
  		bw.write("   public QName[] getHeaders ()");
  		bw.newLine();
  		bw.write("   {");
  		bw.newLine();
  		bw.write("      // Fill in method body");
  		bw.newLine();
  		bw.write("      return null;");
  		bw.newLine();
  		bw.write("   }");
  		bw.newLine();
  		bw.newLine();
  		bw.write("   public boolean handleRequest( MessageContext context )");
  		bw.newLine();
  		bw.write("   {");
  		bw.newLine();
  		bw.write("      // Fill in method body or delete method to use GenericHandler");
  		bw.newLine();
  		bw.write("      return true;");
  		bw.newLine();
  		bw.write("   }");
  		bw.newLine();
  		bw.newLine();
  		bw.write("   public boolean handleResponse( MessageContext context )");
  		bw.newLine();
  		bw.write("   {");
  		bw.newLine();
  		bw.write("      // Fill in method body or delete method to use GenericHandler ");
  		bw.newLine();
  		bw.write("      return true;");
  		bw.newLine();
  		bw.write("   }");
  		bw.newLine();
  		bw.newLine();
  		bw.write("   public boolean handleFault( MessageContext context )");
  		bw.newLine();
  		bw.write("   {");
  		bw.newLine();
  		bw.write("      // Fill in method body or delete method to use GenericHandler");
  		bw.newLine();
  		bw.write("      return true;");
  		bw.newLine();
  		bw.write("   }");
  		bw.newLine();
  		
  		bw.newLine();
  		bw.write("}");
  		bw.close();
  		status = Status.OK_STATUS;	
  	} 
    catch (IOException e) 
    {
  		status = StatusUtils.errorStatus( NLS.bind(ConsumptionUIMessages.MSG_ERROR_WRITE_FILE, new String[]{ className }), e );
  		if (bw != null) {
  			try {
  				bw.close();
  			} catch (IOException e1) {
  			}
  		}
  	}
  	return status;
  }
  
  public void setGenSkeletonEnabled(boolean genSkeleton)
  {
  	this.genSkeleton_ = genSkeleton;
  }
  
  public IProject getProject(){
  	IProject project = null;
  	if (outputLocation_!=null){
  		project = ResourceUtils.getProjectOf(outputLocation_);
  	}
  	return project;
  }
  
  public List getClassNames(){
  	List classes = new ArrayList();
  	if (handlerNameForEdit_!=null){
  		classes.add(handlerNameForEdit_);
  	}
  	return classes;
  }
  
  /**
   * An array of HandlerDescriptionHolders
   * @return
   */
  public void setHandlerServiceRefHolder(HandlerServiceRefHolder[] handlerHolders){
    this.handlerServiceRefHolder_ = handlerHolders;
  }
  
  /**
   * An array of HandlerDescriptionHolders
   * @return
   */
  public void setHandlerDescriptionHolders(HandlerDescriptionHolder[] handlerHolders){
    this.handlerDescriptionHolder_ = handlerHolders;
  }
  
 
}

Back to the top