Skip to main content
summaryrefslogtreecommitdiffstats
blob: e40b3c403ccafb21fb82bfcf7f5b4422b32873d2 (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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*******************************************************************************
 * Copyright (c) 2001, 2008 Oracle 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:
 *     Oracle Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jst.jsf.designtime.internal.view;

import java.util.concurrent.atomic.AtomicBoolean;

import org.eclipse.core.resources.IFile;
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.jst.jsf.common.internal.resource.ImmutableLifecycleListener;
import org.eclipse.jst.jsf.common.runtime.internal.model.component.ComponentTypeInfo;
import org.eclipse.jst.jsf.designtime.context.DTFacesContext;
import org.eclipse.jst.jsf.designtime.internal.view.DTUIViewRoot.StalenessAdvisor;
import org.eclipse.jst.jsf.designtime.internal.view.DTUIViewRoot.StalenessListener;
import org.eclipse.jst.jsf.designtime.internal.view.DTUIViewRoot.VersionStamp;
import org.eclipse.jst.jsf.designtime.internal.view.IDTViewHandler.ViewHandlerException.Cause;
import org.eclipse.wst.common.componentcore.ComponentCore;

/**
 * All IDTViewHandler's must sub-class this abstract class.
 * 
 * @author cbateman
 * 
 */
public abstract class AbstractDTViewHandler implements IDTViewHandler
{
    private final AtomicBoolean _isDisposed    = new AtomicBoolean(false);

    /**
     * the path separator
     */
    public static final String  PATH_SEPARATOR = "/";

    public abstract String calculateLocale(DTFacesContext context)
            throws ViewHandlerException;

    public final DTUIViewRoot createView(final DTFacesContext facesContext,
            final String viewId) throws ViewHandlerException
    {
        if (_isDisposed.get())
        {
            throw new IllegalStateException("View handler is disposed");
        }

        final DTUIViewRoot viewRoot = internalCreateView(facesContext, viewId);
        
        if (viewRoot == null)
        {
            throw new ViewHandlerException("Problem in createView", Cause.UNABLE_TO_CREATE_VIEW);
        }
        viewRoot.setViewId(viewId);
        final VersionStamp versionStamp = createVersionStamp(facesContext,
                viewId);
        if (versionStamp == null)
        {
            throw new ViewHandlerException(new Throwable(
                    "Bad version stamp detected"), Cause.BAD_VERSION_STAMP);
        }
        viewRoot.setVersionStamp(versionStamp);

        final StalenessAdvisor advisor = createStalenessAdvisor(viewRoot,
                facesContext, viewId);

        if (advisor == null)
        {
            throw new ViewHandlerException(new Throwable(
                    "Bad staleness advisor"), Cause.BAD_STALENESS_ADVISOR);
        }
        viewRoot.setStalenessAdvisor(advisor);

        // lock down the tree
        viewRoot.setSubtreeProtected();

        registerView(viewRoot, facesContext, viewId);

        return viewRoot;
    }

    /**
     * @param facesContext
     * @param viewId
     * @return internal construction of the view root.
     */
    protected abstract DTUIViewRoot internalCreateView(
            final DTFacesContext facesContext, final String viewId);

    public abstract IResource getActionDefinition(DTFacesContext context,
            String viewId) throws ViewHandlerException;

    public abstract IPath getActionURL(DTFacesContext context,
            IResource resource, IPath requestPath) throws ViewHandlerException;

    public abstract IPath getRelativeActionPath(DTFacesContext context,
            String relativeToViewId, String uri) throws ViewHandlerException;

    public abstract IViewDefnAdapterFactory getViewMetadataAdapterFactory(
            DTFacesContext context) throws ViewHandlerException;

    public abstract boolean supportsViewDefinition(IFile file);

    
    public abstract void setLifecycleListener(ImmutableLifecycleListener listener);

    public final void dispose()
    {
        if (_isDisposed.compareAndSet(false, true))
        {
            doDispose();
        }
    }

    /**
     * Sub-classes should override to add disposal tasks
     */
    protected void doDispose()
    {
        // do nothing by default
    }

    /**
     * IMPORTANT: this method must not return null. All view creation will fail
     * for this handler when createVersionStamp() returns null. create() will
     * throw ViewHandlerException.
     * 
     * @param facesContext
     * @param viewId
     * @return a new version stamp for the facesContext/viewId. The version
     *         stamp needs to be unique for all instances of viewId only, not
     *         all instances of component trees everywhere in the system.
     */
    protected abstract VersionStamp createVersionStamp(
            final DTFacesContext facesContext, final String viewId);

    /**
     * IMPORTANT: this method must not return null. All view creation will fail
     * for this handler when createStalenessAdvisor returns null.
     * 
     * @param viewRoot
     * @param facesContext
     * @param viewId
     * @return a new staleness advisor.
     */
    protected abstract StalenessAdvisor createStalenessAdvisor(
            final DTUIViewRoot viewRoot, DTFacesContext facesContext,
            String viewId);

    /**
     * Called by createView immediately before returning.  Provides sub-class
     * a chance to register a fully initialized view root with any stateful
     * activities such as automatic change tracking and caching.
     * 
     * @param viewRoot
     * @param context
     * @param viewId
     */
    protected void registerView(DTUIViewRoot viewRoot, final DTFacesContext context, final String viewId)
    {
        // do nothing by default
    }

    public String getViewId(final DTFacesContext context, final IResource res)
    {
        // TODO: sync with WebrootUtil?
        String strWebrootPath = "";
        final IProject project = res.getProject();
        final IPath path = res.getFullPath();
        final IPath webContentPath = getWebContentPath(project);
        if (webContentPath != null && webContentPath.isPrefixOf(path))
        {
            final int start = path.matchingFirstSegments(webContentPath);
            final String[] segments = path.segments();
            for (int i = start, n = path.segmentCount(); i < n; i++)
            {
                strWebrootPath = strWebrootPath + PATH_SEPARATOR + segments[i];
            }
        }
        return strWebrootPath;
    }

    private IPath getWebContentPath(final IProject project)
    {
        if (project != null)
        {
            return ComponentCore.createComponent(project).getRootFolder()
                    .getUnderlyingFolder().getFullPath();
        }
        return null;
    }

    /**
     * A default DTUIViewRoot that represents an uncreatable or uninitializable
     * view root.
     *
     */
    protected static final class NullViewRoot extends DTUIViewRoot
    {
        /**
         * 
         */
        private static final long serialVersionUID = 1187035338610719171L;

        /**
         */
        protected NullViewRoot()
        {
            super(null, null, new ComponentTypeInfo("", "", "",""));
        }

        @Override
        public IAdaptable getServices()
        {
            return new IAdaptable()
            {
                public Object getAdapter(Class adapter)
                {
                    // no services
                    return null;
                }
            };
        }
        
    }

    /**
     * Provides a simple time-based stamp that makes almost impossible to create
     * the same timestamp twice. To do so, two threads would need to get the
     * same system simulataneously and get same number back from the same
     * gaussian distribution.
     * 
     * @author cbateman
     * 
     */
    protected static class TimeBasedVersionStamp extends VersionStamp
    {
        /**
         * 
         */
        private static final long serialVersionUID = 5557828245936568977L;
        private final long        timestamp;
        private final long        randomStamp;

        /**
         * 
         */
        public TimeBasedVersionStamp()
        {
            super();
            // use the millisecond time, since it is offset from a known
            // point on all platform, whereas no guarantee is made about
            // the nano time value.
            this.timestamp = System.currentTimeMillis();
            // this protects against two threads constructing two time
            // stamp objects "simulataneously" and having the same time based
            // stamp. Math.random is thread-safe.
            this.randomStamp = (long) (Long.MAX_VALUE * Math.random());
        }

        @Override
        public int hashCode()
        {
            return (int) (timestamp ^ randomStamp);
        }

        @Override
        protected boolean isEqual(final VersionStamp other)
        {
            return other instanceof TimeBasedVersionStamp
                    && (timestamp == ((TimeBasedVersionStamp) other).timestamp)
                    && (randomStamp == ((TimeBasedVersionStamp) other).randomStamp);
        }
    }

    /**
     * Returns a default staleness advisor that always returns false.
     * 
     * @author cbateman
     * 
     */
    protected static final class NullStalenessAdvisor extends StalenessAdvisor
    {
        /**
         * 
         */
        private static final long serialVersionUID = 5946420948320047613L;

        @Override
        public boolean isStale()
        {
            return false;
        }

        @Override
        public void addListener(final StalenessListener listener)
        {
            // do nothing
        }

        @Override
        public void removeListener(final StalenessListener listener)
        {
            // do nothing
        }
        
        public boolean isAccessible()
        {
            return true;
        }
    }
}

Back to the top