Skip to main content
summaryrefslogtreecommitdiffstats
blob: 4ebaf35ac2da349049a41e8f4c5739ee3d58a5fa (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
/*******************************************************************************
 * Copyright (c) 2003, 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
 *******************************************************************************/
package org.eclipse.wst.common.internal.emf.utilities;


import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;


/**
 * @deprecated This class will be deleted. If you still need to use this class, please contact the
 *             WCCM team. A class which can be used in conjunction with Encoder/Decoders to save
 *             resources if they are made dirty by automatic encoding support. Usage Example:
 * 
 * <pre>
 * 
 * 
 * // Use the standard WebSphere password value encoder/decoder.
 * EncoderDecoderRegistry.getDefaultRegistry().setDefaultEncoderDecoder(new com.ibm.ejs.security.util.WASEncoderDecoder());
 * // Begin tracking changes...
 * WriteBackHelper.begin();
 * // Load a resource which may have un-encoded values...
 * // Note: The WCCM will attempt to detect un-encoded values.  If unencoded values
 * // are found, the value will be encoded, and the resource will be added to the
 * // WriteBackHelper.
 * Resource res = resourceSet.load(&quot;myResource&quot;);
 * // Ensure that any changes due to encoding are written back out.
 * WriteBackHelper.end();
 * </pre>
 */
public class WriteBackHelper {
	private Set dirtyObjects = new HashSet();
	private boolean trackingChanges = false;
	private static WriteBackHelper _instance;
	static {
		//Deprecated class
		Revisit.deleteMe();
	}

	/**
	 * Private constructor ensures proper usage through singleton.
	 */
	private WriteBackHelper() {
		super();
	}

	/**
	 * Adds a resource which is dirty, and needs to be saved.
	 */
	public void addDirtyObject(EObject dirtyObject) {
		dirtyObjects.add(dirtyObject);
	}

	/**
	 * Begin collecting objects which have changed.
	 */
	public void begin() {
		trackingChanges = true;
	}

	/**
	 * Attempts to save all dirty resources (if possible), then marks the resources as non-dirty.
	 */
	public void end() {
		saveDirtyResources();
		reset();
		trackingChanges = false;
	}

	/**
	 * Returns true if changes to mof objects are currently being tracked.
	 */
	public boolean isActive() {
		return trackingChanges;
	}

	/**
	 * Clears the list of dirty resources.
	 */
	protected void reset() {
		dirtyObjects.clear();
	}

	/**
	 * Attempts to save all dirty resources (if possible), then marks the resources as non-dirty.
	 */
	protected void saveDirtyResources() {
		Set dirtyResources = new HashSet();
		Iterator dirtyObjIter = dirtyObjects.iterator();
		while (dirtyObjIter.hasNext()) {
			EObject dirtyObject = (EObject) dirtyObjIter.next();
			if (dirtyObject.eResource() != null && !dirtyResources.contains(dirtyObject)) {
				dirtyResources.add(dirtyObject.eResource());
			}
		}
		Iterator dirtyIter = dirtyResources.iterator();
		while (dirtyIter.hasNext()) {
			Resource dirtyResource = (Resource) dirtyIter.next();
			try {
				dirtyResource.save(Collections.EMPTY_MAP);
			} catch (Exception e) {
				warn(dirtyResource, e);
			}
		}
	}

	/**
	 * Adds a resource which is dirty, and needs to be saved.
	 */
	public static WriteBackHelper singleton() {
		if (_instance == null) {
			_instance = new WriteBackHelper();
		}
		return _instance;
	}

	/**
	 * Warn the user of problems during save.
	 */
	protected void warn(Resource res, Exception e) {
		System.err.println(WFTUtilsResourceHandler.getString(WFTUtilsResourceHandler.Warning__Could_not_write_b_WARN_, new Object[]{res.getURI()})); //$NON-NLS-1$
	}
}

Back to the top