Skip to main content
summaryrefslogtreecommitdiffstats
blob: 3cf7e82548a67f3e6ad08e0c323b274591c4373c (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
/*******************************************************************************
 * Copyright (c) 2010 Shane Clarke.
 * 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:
 *    Shane Clarke - initial API and implementation
 *******************************************************************************/
package org.eclipse.jst.ws.internal.jaxws.ui.wizards;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import javax.jws.HandlerChain;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.jobs.ISchedulingRule;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.emf.common.util.URI;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.Annotation;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.MemberValuePair;
import org.eclipse.jface.wizard.IWizardPage;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.jst.ws.annotations.core.AnnotationsCore;
import org.eclipse.jst.ws.annotations.core.utils.AnnotationUtils;
import org.eclipse.jst.ws.internal.jaxws.core.utils.JAXWSHandlerUtils;
import org.eclipse.jst.ws.internal.jaxws.ui.JAXWSUIMessages;
import org.eclipse.jst.ws.internal.jaxws.ui.JAXWSUIPlugin;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.actions.WorkspaceModifyOperation;

public class ConfigureHandlerWizard extends Wizard {
    private AddHandlerChainPage addHandlerChainPage;
    private OrderHandlerChainPage orderHandlerChainPage;

    private boolean fileCreated;
    private boolean addAnnotation;

    private IPath handlerChainPath;
    private IType type;

    public ConfigureHandlerWizard(IType type) {
        this.type = type;
        setWindowTitle(JAXWSUIMessages.JAXWS_CONFIGURE_HANDLER_WIZARD_TITLE);
        //TODO replace with dedicated handler wizban
        setDefaultPageImageDescriptor(JAXWSUIPlugin.getImageDescriptor("$nl$/icons/wizban/new_wiz.png"));  //$NON-NLS-1$
        handlerChainPath = getHandlerChainPath(type);
    }

    @Override
    public void addPages() {
        if (addHandlerChainPage == null) {
            addHandlerChainPage = new AddHandlerChainPage(type);
        }
        if (orderHandlerChainPage == null) {
            orderHandlerChainPage = new OrderHandlerChainPage(handlerChainPath, type.getJavaProject());
        }
        if (handlerChainPath != null && handlerChainPath.isEmpty()) {
            addPage(addHandlerChainPage);
        }
        addPage(orderHandlerChainPage);
    }

    @Override
    public IWizardPage getNextPage(IWizardPage page) {
        if (page == addHandlerChainPage && addHandlerChainPage.isPageComplete()) {
            if (addHandlerChainPage.isCreateHandlerChain()) {
                handlerChainPath = new Path(addHandlerChainPage.getNewHandlerChainPath());
                orderHandlerChainPage.setHandlerChainPath(handlerChainPath);
                addAnnotation = true;
                fileCreated = true;
                addHandlerChainPage.setFileCreated(fileCreated);
            }
            if (addHandlerChainPage.isEditHandlerChain()) {
                handlerChainPath = new Path(addHandlerChainPage.getExistingHandlerChainPath());
                orderHandlerChainPage.setHandlerChainPath(handlerChainPath);
                addAnnotation = true;
                fileCreated = false;
                addHandlerChainPage.setFileCreated(fileCreated);
            }
        }
        return super.getNextPage(page);
    }

    private IPath getHandlerChainPath(IType type) {
        if (type != null) {
            Annotation handlerChain = AnnotationUtils.getAnnotation(type, HandlerChain.class);
            if (handlerChain != null) {
                String file = AnnotationUtils.getStringValue(handlerChain, "file"); //$NON-NLS-1$
                if (file != null) {
                    if (!file.startsWith("../")) { //$NON-NLS-1$
                        return type.getPackageFragment().getPath().append(new Path(file));
                    } else {
                        return findHandlerChainPath(file);
                    }
                }
            }
        }
        return Path.EMPTY;
    }

    private IPath findHandlerChainPath(String file) {
        IContainer container = type.getResource().getParent();
        if (container != null) {
            while(file.startsWith("../")) { //$NON-NLS-1$
                file = file.substring(3);
                if (container != null) {
                    container = container.getParent();
                }
            }
            if (container != null) {
                IResource handlerChainFile = container.findMember(file);
                if (handlerChainFile != null) {
                    return handlerChainFile.getFullPath();
                }
            }
        }
        return Path.EMPTY;
    }

    @Override
    public boolean performFinish() {
        try {

            ISchedulingRule rule = null;
            Job job = Job.getJobManager().currentJob();
            if (job != null) {
                rule = job.getRule();
            } else {
                rule = ResourcesPlugin.getWorkspace().getRoot();
            }
            WorkspaceModifyOperation workspaceModifyOperation = new WorkspaceModifyOperation(rule) {

                @Override
                protected void execute(IProgressMonitor monitor) throws CoreException, InvocationTargetException, InterruptedException {
                    if (addAnnotation) {
                        IFile handlerChainFile = ResourcesPlugin.getWorkspace().getRoot().getFile(handlerChainPath);

                        URI relativeURI = URI.createPlatformResourceURI(handlerChainFile.getLocation().toOSString(),
                                false).deresolve(
                                        URI.createPlatformResourceURI(type.getResource().getLocation().toOSString(),
                                                false));

                        ICompilationUnit compilationUnit = type.getCompilationUnit();
                        ASTParser parser = ASTParser.newParser(AST.JLS3);
                        parser.setSource(compilationUnit);
                        CompilationUnit cu = (CompilationUnit)parser.createAST(null);
                        List<MemberValuePair> memberValuePairs = new ArrayList<MemberValuePair>();
                        MemberValuePair filePair = AnnotationsCore.createStringMemberValuePair(cu.getAST(),
                                "file", relativeURI.toFileString());  //$NON-NLS-1$
                        memberValuePairs.add(filePair);
                        Annotation handlerChainAnnotation = AnnotationsCore.createNormalAnnotation(cu.getAST(),
                                HandlerChain.class.getSimpleName(), memberValuePairs);
                        AnnotationUtils.addAnnotation(type, handlerChainAnnotation);
                        AnnotationUtils.addImport(type, HandlerChain.class.getCanonicalName());
                    }
                    JAXWSHandlerUtils.writeDocumentToFile(handlerChainPath, orderHandlerChainPage.getDocument());
                }
            };
            PlatformUI.getWorkbench().getProgressService().runInUI(getContainer(), workspaceModifyOperation, rule);
        } catch (InvocationTargetException ite) {
            JAXWSUIPlugin.log(ite);
            return false;
        } catch  (InterruptedException ie) {
            JAXWSUIPlugin.log(ie);
            return false;
        }
        return true;
    }

    public void deleteFile(IPath filePath) {
        IFile handlerFile = ResourcesPlugin.getWorkspace().getRoot().getFile(filePath);
        if (handlerFile.exists()) {
            try {
                handlerFile.delete(true, null);
            } catch (CoreException ce) {
                JAXWSUIPlugin.log(ce.getStatus());
            }
        }
    }
    
    @Override
    public boolean performCancel() {
        if (fileCreated) {
            IPath filePath = type.getPackageFragment().getPath().append(new Path("handler-chain.xml")); //$NON-NLS-1$
            deleteFile(filePath);
        }
        return super.performCancel();
    }

}

Back to the top