Skip to main content
summaryrefslogtreecommitdiffstats
blob: 3b05dcb7170c47563eced5f7633ce68b606a86ec (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
/*******************************************************************************
 * Copyright (c) 2005 Oracle Corporation.
 * 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:
 *    Ian Trimble - initial API and implementation
 *******************************************************************************/ 
package org.eclipse.jst.jsf.core.internal.provisional.jsfappconfig;

import java.io.IOException;
import java.util.Collections;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.jst.jsf.core.internal.JSFCorePlugin;
import org.eclipse.jst.jsf.core.internal.Messages;
import org.eclipse.jst.jsf.facesconfig.emf.FacesConfigType;
import org.eclipse.jst.jsf.facesconfig.util.FacesConfigResourceFactory;
import org.eclipse.osgi.util.NLS;
import org.eclipse.wst.common.internal.emf.resource.EMF2SAXRendererFactory;

/**
 * JARFileJSFAppConfigProvider provides the root element of an application
 * configuration model by loading the model from a /META-INF/faces-config.xml
 * entry in a JAR file.
 * 
 * @author Ian Trimble - Oracle
 */
public class JARFileJSFAppConfigProvider extends AbstractJSFAppConfigProvider {

	/**
	 * Prefix required to turn filename into a JAR URI.
	 */
	public static final String JARFILE_URI_PREFIX = "jar:file:///";

	/**
	 * Suffix required to turn filename into a JAR URI.
	 */
	public static final String FACES_CONFIG_IN_JAR_SUFFIX = "!/META-INF/faces-config.xml";

	/**
	 * Name of a JAR file that contains a /META-INF/faces-config.xml entry.
	 */
	protected String filename = null;

	/**
	 * Cached {@link FacesConfigType} instance.
	 */
	protected FacesConfigType facesConfig = null;

	/**
	 * Flag to track if load error has been logged at least once.
	 */
	protected boolean loadErrorLogged = false;

	/**
	 * Creates an instance, storing the passed IProject instance and file name
	 * String to be used for subsequent processing.
	 * 
	 * @param project IProject instance who's classpath contains this JAR file.
	 * @param filename Name of a JAR file that contains a
	 * /META-INF/faces-config.xml entry.
	 */
	public JARFileJSFAppConfigProvider(String filename) {
		this.filename = filename;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jst.jsf.core.internal.provisional.jsfappconfig.IJSFAppConfigProvider#getFacesConfigModel()
	 */
	public FacesConfigType getFacesConfigModel() {
		if (facesConfig == null) {
			if (filename != null) {
				StringBuffer sb = new StringBuffer();
				sb.append(JARFILE_URI_PREFIX);
				sb.append(filename);
				sb.append(FACES_CONFIG_IN_JAR_SUFFIX);
				URI jarFileURI = URI.createURI(sb.toString());
				FacesConfigResourceFactory resourceFactory = new FacesConfigResourceFactory(EMF2SAXRendererFactory.INSTANCE);
				Resource resource = resourceFactory.createResource(jarFileURI);
				try {
					resource.load(Collections.EMPTY_MAP);
					if (resource != null) {
						EList resourceContents = resource.getContents();
						if (resourceContents != null && resourceContents.size() > 0) {
							facesConfig = (FacesConfigType)resourceContents.get(0);
							if (facesConfig != null) {
								jsfAppConfigLocater.getJSFAppConfigManager().addFacesConfigChangeAdapter(facesConfig);
							}
						}
					}
				} catch(IllegalStateException ise) {
					//log error
					logLoadError(ise);
				} catch(IOException ioe) {
					//log error
					logLoadError(ioe);
				}
			}
		}
		return facesConfig;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jst.jsf.core.internal.provisional.jsfappconfig.IJSFAppConfigProvider#releaseFacesConfigModel()
	 */
	public void releaseFacesConfigModel() {
		jsfAppConfigLocater.getJSFAppConfigManager().removeFacesConfigChangeAdapter(facesConfig);
	}

	/**
	 * Called to log a load error; load error will be logged once only per
	 * instance, per VM session.
	 * 
	 * @param ex Throwable instance to be logged.
	 */
	protected void logLoadError(Throwable ex) {
		if (!loadErrorLogged) {
			JSFCorePlugin.log(
					IStatus.ERROR,
					NLS.bind(Messages.JARFileJSFAppConfigProvider_ErrorLoadingModel, JARFILE_URI_PREFIX + filename + FACES_CONFIG_IN_JAR_SUFFIX),
					ex);
			loadErrorLogged = true;
		}
	}

	/*
	 * (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	public boolean equals(Object otherObject) {
		boolean equals = false;
		if (otherObject != null && otherObject instanceof JARFileJSFAppConfigProvider) {
			String otherFilename = ((JARFileJSFAppConfigProvider)otherObject).filename;
			if (filename != null) {
				equals = filename.equals(otherFilename);
			} else {
				equals = otherFilename == null;
			}
		}
		return equals;
	}

	/*
	 * (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	public int hashCode() {
		return filename != null ? filename.hashCode() : 0;
	}

	/*
	 * (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		StringBuffer sb = new StringBuffer("JARFileJSFAppConfigProvider["); //$NON-NLS-1$
		if (filename != null) {
			sb.append(filename);
		} else {
			sb.append("null"); //$NON-NLS-1$
		}
		sb.append("]"); //$NON-NLS-1$
		return sb.toString();
	}

}

Back to the top