Revision 38961

View differences:

tags/v2_0_0_Build_2055/libraries/libFMap_dal/resources-test/log4j.xml
1
<?xml version="1.0" encoding="UTF-8" ?>
2
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
3

  
4
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
5

  
6
    <appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
7
        <layout class="org.apache.log4j.PatternLayout">
8
            <param name="ConversionPattern" value="%d{HH:mm:ss,SSS} %-5p [%c{2}.%M()]\n  %m%n"/>
9
        </layout>
10
    </appender>
11

  
12
  <category name="org.gvsig.fmap.dal">
13
    <priority value="WARN"/>
14
  </category>
15

  
16
    <root>
17
        <priority value="INFO" />
18
        <appender-ref ref="CONSOLE" />
19
    </root>
20
</log4j:configuration>
0 21

  
tags/v2_0_0_Build_2055/libraries/libFMap_dal/src/org/gvsig/fmap/dal/DataServerExplorerParameters.java
1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 */
22

  
23
/*
24
* AUTHORS (In addition to CIT):
25
* 2008 IVER T.I. S.A.   {{Task}}
26
*/
27

  
28
package org.gvsig.fmap.dal;
29

  
30
/**
31
 * DataServerExplorer parameter container. Provides a way to obtain the name 
32
 * of the specific data explorer type described by this parameters.
33
 * 
34
 */
35
public interface DataServerExplorerParameters extends DataParameters {
36

  
37
	/**
38
	 * Returns the name of the data explorer type of the DataServerExplorer
39
	 * represented by this DataServerExplorerParameters
40
	 * 
41
	 * @return a String containing the name of the data explorer type
42
	 */
43
	public String getExplorerName();
44

  
45
}
tags/v2_0_0_Build_2055/libraries/libFMap_dal/src/org/gvsig/fmap/dal/resource/Resource.java
1
package org.gvsig.fmap.dal.resource;
2

  
3
import org.gvsig.fmap.dal.resource.exception.AccessResourceException;
4
import org.gvsig.fmap.dal.resource.exception.ResourceException;
5
import org.gvsig.fmap.dal.resource.exception.ResourceExecuteException;
6
import org.gvsig.fmap.dal.resource.spi.ResourceConsumer;
7

  
8
/**
9
 * Encapsulates a system resource (file, database connection, etc). 
10
 * It is used to manage usage and availability of shared system 
11
 * resources. This interface allows monitoring a resource and helps 
12
 * preventing dead locks on it as well as being freed as soon as
13
 * it is not being used.
14
 * 
15
 * Data providers can provide implementations for their own resources. This
16
 * is specially interesting when resources require a specific treatment
17
 * beyond the standard shared file or connection, for instance to manage 
18
 * connections to a server through its own connection pool.
19
 */
20
public interface Resource {
21

  
22
	/**
23
	 * Returns the resource's name.
24
	 * 
25
	 * @return resource's name
26
	 * 
27
	 * @throws AccessResourceException
28
	 */
29
	public String getName() throws AccessResourceException;
30

  
31
	/**
32
	 * Returns the resource parameters. These parameters contain
33
	 * all the necessary information to access the resource.
34
	 * 
35
	 * @return resource parameters.
36
	 */
37
	public ResourceParameters getParameters();
38

  
39
	/**
40
	 * Returns the date and time in which this resource was opened for the last
41
	 * time.
42
	 * 
43
	 * @return date and time in which this resource was opened for the last
44
	 *         time, in milliseconds.
45
	 */
46
	public long getLastTimeOpen();
47

  
48
	/**
49
	 * Returns the date and time in which this resource was accessed for the
50
	 * last time.
51
	 * 
52
	 * @return date and time in which this resource was accessed for the last
53
	 *         time, in milliseconds.
54
	 */
55
	public long getLastTimeUsed();
56

  
57
	/**
58
	 * Returns whether this resource is already in use by someone.
59
	 * 
60
	 * @return
61
	 * 		true if this resource is in use, false if not.
62
	 */
63
	public boolean inUse();
64

  
65
	/**
66
	 * Returns whether this resource is opened.
67
	 * 
68
	 * @return
69
	 * 		true if this resource is opened, false if not.
70
	 */
71
	public boolean isOpen();
72

  
73
	/**
74
	 * Returns the number of times this resource has been opened 
75
	 * since it was created.
76
	 * 
77
	 * @return 
78
	 * 		number of times this resource has been opened 
79
	 * since it was created.
80
	 */
81
	public int openCount();
82

  
83
	/**
84
	 * Executes an action which uses the current {@link Resource}.
85
	 * 
86
	 * @param runnable
87
	 *            to execute
88
	 * @return the action return value
89
	 * @throws ResourceException
90
	 *             if there is an error executing the action
91
	 */
92
	public Object execute(ResourceAction action)
93
			throws ResourceExecuteException;
94

  
95
	/**
96
	 * If the resource is not in use, calling this method will send a close request 
97
	 * to all consumers referencing this resource. If the resource is in use, 
98
	 * calling this method will do nothing.
99
	 * 
100
	 * @throws ResourceException
101
	 */
102
	public void closeRequest() throws ResourceException;
103

  
104
	/**
105
	 * Adds a consumer to this resource. This will create a weak reference 
106
	 * to the consumer in this resource's consumer list.
107
	 * 
108
	 * @param consumer
109
	 * 				the consumer that will be added to this resource's consumer list.
110
	 */
111
	public void addConsumer(ResourceConsumer consumer);
112

  
113
	/**
114
	 * Removes a consumer from this resource's consumer list.
115
	 * 
116
	 * @param consumer
117
	 * 				the consumer that will be removed.
118
	 */
119
	public void removeConsumer(ResourceConsumer consumer);
120

  
121
	/**
122
	 * Returns this resource's current number of consumers.
123
	 * 
124
	 * @return
125
	 * 		current number of consumers of this resource.
126
	 */
127
	public int getConsumersCount();
128
	
129
	/**
130
	 * Returns an object that represents the resource. The actual type
131
	 * depends on the resource provider. It could be a string with a 
132
	 * descriptive name or something more elaborated.
133
	 * 
134
	 * @return
135
	 * 		an object that represents the resource.
136
	 * 
137
	 * @throws AccessResourceException
138
	 */
139
	public Object get() throws AccessResourceException;
140

  
141
	/**
142
	 * Returns a custom object containing extended data relative to 
143
	 * this resource.
144
	 * 
145
	 * This is part of a simple mechanism to allow passing further data to 
146
	 * the resource that may be necessary for optimal treatment.
147
	 * 
148
	 * @return
149
	 * 		data related to this resource
150
	 */
151
	public Object getData();
152

  
153
	/**
154
	 * Sets a custom object as this resource's extended data.
155
     *
156
	 * This is part of a simple mechanism to allow passing further data to 
157
	 * the resource that may be necessary for optimal treatment.
158
	 *
159
	 * @param data
160
	 * 			a custom object containing data related to this resource.
161
	 */
162
	public void setData(Object data);
163

  
164
}
tags/v2_0_0_Build_2055/libraries/libFMap_dal/src/org/gvsig/fmap/dal/resource/ResourceParameters.java
1
package org.gvsig.fmap.dal.resource;
2

  
3
import org.gvsig.fmap.dal.DataParameters;
4

  
5
/**
6
 * Interface that contains any resource parameters.
7
 * Each specific subtype of resource will extend this
8
 * interface with its own relevant parameters.
9
 *
10
 */
11
public interface ResourceParameters extends DataParameters {
12

  
13
	/**
14
	 * Returns the type name of the related resource.
15
	 * 
16
	 * @return
17
	 * 		type name of the resource.
18
	 */
19
	public String getTypeName();
20

  
21
}
22

  
0 23

  
tags/v2_0_0_Build_2055/libraries/libFMap_dal/src/org/gvsig/fmap/dal/resource/spi/MultiResource.java
1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 * 
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 * 
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 * 
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
19
 * MA  02110-1301, USA.
20
 * 
21
 */
22

  
23
/*
24
 * AUTHORS (In addition to CIT):
25
 * 2009 {}  {{Task}}
26
 */
27
package org.gvsig.fmap.dal.resource.spi;
28

  
29
import java.util.ArrayList;
30
import java.util.List;
31

  
32
import org.gvsig.fmap.dal.DALLocator;
33
import org.gvsig.fmap.dal.exception.InitializeException;
34
import org.gvsig.fmap.dal.resource.ResourceParameters;
35
import org.gvsig.fmap.dal.resource.exception.AccessResourceException;
36
import org.gvsig.fmap.dal.resource.exception.PrepareResourceException;
37
import org.gvsig.fmap.dal.resource.exception.ResourceException;
38
import org.gvsig.fmap.dal.resource.exception.ResourceNotifyChangesException;
39
import org.gvsig.fmap.dal.resource.exception.ResourceNotifyCloseException;
40
import org.gvsig.fmap.dal.resource.exception.ResourceNotifyDisposeException;
41
import org.gvsig.fmap.dal.resource.exception.ResourceNotifyOpenException;
42
import org.slf4j.Logger;
43
import org.slf4j.LoggerFactory;
44

  
45
/**
46
 * Resource implementation which is able to show an unique interface to a group
47
 * of Resources.
48
 * 
49
 * @author 2009- <a href="cordinyana@gvsig.org">C?sar Ordi?ana</a> - gvSIG team
50
 */
51
public class MultiResource extends AbstractResource {
52

  
53
	public static final String TYPE_NAME = "multi";
54

  
55
	public static final String DESCRIPTION = "Group of multiple resources";
56

  
57
	private static final Logger LOG =
58
			LoggerFactory.getLogger(MultiResource.class);
59

  
60
	private List resources = new ArrayList();
61

  
62
	// Initially, the first one
63
	private int defaultResourcePos = 0;
64

  
65
	/**
66
	 * Creates a new {@link MultiResource} from the given parameters.
67
	 * 
68
	 * @param parameters
69
	 *            to create the resource from
70
	 * @throws InitializeException
71
	 *             if there is an error creating the resource
72
	 */
73
	public MultiResource(MultiResourceParameters parameters)
74
			throws InitializeException {
75
		super(parameters);
76
	}
77

  
78
	private List getResources() {
79
		return resources;
80
	}
81

  
82
	/**
83
	 * Adds a new {@link ResourceProvider} to the list of Resources managed by
84
	 * this group.
85
	 * 
86
	 * @param parameters
87
	 *            to create the resource from
88
	 * @param defaultResource
89
	 *            if the added resource is to be used as the multi resource
90
	 *            default one
91
	 * @throws InitializeException
92
	 *             if there is an error adding the resource
93
	 */
94
	public void addResource(ResourceParameters parameters,
95
			boolean defaultResource) throws InitializeException {
96
		ResourceProvider resourceProvider =
97
				((ResourceManagerProviderServices) DALLocator.getResourceManager()).createResource(parameters);
98
		resources.add(resourceProvider);
99
		if (defaultResource) {
100
			defaultResourcePos = resources.size() - 1;
101
		}
102
	}
103

  
104
	/**
105
	 * Adds a new {@link ResourceProvider} to the list of Resources managed by
106
	 * this group.
107
	 * 
108
	 * @param name
109
	 *            of the resource to add
110
	 * @param params
111
	 *            parameters to create the resource with
112
	 * @param defaultResource
113
	 *            if the added resource is to be used as the multi resource
114
	 *            default one
115
	 * @throws InitializeException
116
	 *             if there is an error adding the resource
117
	 */
118
	public void addResource(String name, Object[] params,
119
			boolean defaultResource) throws InitializeException {
120
		ResourceProvider resourceProvider =
121
				((ResourceManagerProviderServices) DALLocator.getResourceManager()).createResource(
122
						name, params);
123
		resources.add(resourceProvider);
124
		if (defaultResource) {
125
			defaultResourcePos = resources.size() - 1;
126
		}
127
	}
128

  
129
	public boolean isThis(ResourceParameters parameters)
130
			throws ResourceException {
131
		if (parameters instanceof MultiResourceParameters) {
132
			MultiResourceParameters multiParams =
133
					(MultiResourceParameters) parameters;
134
			return multiParams.getName() != null
135
					&& multiParams.getName().equals(
136
							getMultiResourceParameters().getName());
137
		}
138

  
139
		return false;
140
	}
141

  
142
	public void notifyChanges() throws ResourceNotifyChangesException {
143
		List resources = getResources();
144
		for (int i = 0; i < resources.size(); i++) {
145
			((ResourceProvider) getResources().get(i)).notifyChanges();
146
		}
147
	}
148

  
149
	public void notifyClose() throws ResourceNotifyCloseException {
150
		List resources = getResources();
151
		for (int i = 0; i < resources.size(); i++) {
152
			((ResourceProvider) getResources().get(i)).notifyClose();
153
		}
154
	}
155

  
156
	public void notifyDispose() throws ResourceNotifyDisposeException {
157
		List resources = getResources();
158
		for (int i = 0; i < resources.size(); i++) {
159
			((ResourceProvider) getResources().get(i)).notifyDispose();
160
		}
161
	}
162

  
163
	public void notifyOpen() throws ResourceNotifyOpenException {
164
		List resources = getResources();
165
		for (int i = 0; i < resources.size(); i++) {
166
			((ResourceProvider) getResources().get(i)).notifyOpen();
167
		}
168
	}
169

  
170
	public void prepare() throws PrepareResourceException {
171
		List resources = getResources();
172
		for (int i = 0; i < resources.size(); i++) {
173
			((ResourceProvider) getResources().get(i)).prepare();
174
		}
175
	}
176

  
177
	public void closeRequest() throws ResourceException {
178
		List resources = getResources();
179
		for (int i = 0; i < resources.size(); i++) {
180
			((ResourceProvider) getResources().get(i)).closeRequest();
181
		}
182
	}
183

  
184
	public Object get() throws AccessResourceException {
185
		ResourceProvider defaultResource = getDefaultResource();
186
		if (defaultResource == null) {
187
			return null;
188
		} else {
189
			return defaultResource.get();
190
		}
191
	}
192

  
193
	public Object getData() {
194
		ResourceProvider defaultResource = getDefaultResource();
195
		if (defaultResource == null) {
196
			return null;
197
		} else {
198
			return defaultResource.getData();
199
		}
200
	}
201

  
202
	private MultiResourceParameters getMultiResourceParameters() {
203
		return (MultiResourceParameters) getParameters();
204
	}
205

  
206
	public String getName() throws AccessResourceException {
207
		StringBuffer buffer =
208
				new StringBuffer().append("MultiResource ").append(
209
						getMultiResourceParameters().getName()).append(":: ");
210
		List resources = getResources();
211
		for (int i = 0; i < resources.size(); i++) {
212
			buffer.append(((ResourceProvider) resources.get(i)).getName());
213
			if (defaultResourcePos == i) {
214
				buffer.append(" (default)");
215
			}
216
			if (i < resources.size() - 1) {
217
				buffer.append(", ");
218
			}
219
		}
220
		return buffer.toString();
221
	}
222

  
223
	public String toString() {
224
		try {
225
			return getName();
226
		} catch (AccessResourceException e) {
227
			LOG.error("Error getting the resource name", e);
228
		}
229
		return super.toString();
230
	}
231

  
232
	private ResourceProvider getDefaultResource() {
233
		if (getResources().size() > 0) {
234
			return (ResourceProvider) getResources().get(defaultResourcePos);
235
		} else {
236
			return null;
237
		}
238
	}
239

  
240
	public void setData(Object data) {
241
		ResourceProvider defaultResource = getDefaultResource();
242
		if (defaultResource != null) {
243
			defaultResource.setData(data);
244
		}
245
	}
246
}
tags/v2_0_0_Build_2055/libraries/libFMap_dal/src/org/gvsig/fmap/dal/resource/spi/ResourceProvider.java
1
package org.gvsig.fmap.dal.resource.spi;
2

  
3
import org.gvsig.fmap.dal.resource.Resource;
4
import org.gvsig.fmap.dal.resource.ResourceParameters;
5
import org.gvsig.fmap.dal.resource.exception.PrepareResourceException;
6
import org.gvsig.fmap.dal.resource.exception.ResourceException;
7
import org.gvsig.fmap.dal.resource.exception.ResourceNotifyChangesException;
8
import org.gvsig.fmap.dal.resource.exception.ResourceNotifyCloseException;
9
import org.gvsig.fmap.dal.resource.exception.ResourceNotifyDisposeException;
10
import org.gvsig.fmap.dal.resource.exception.ResourceNotifyOpenException;
11

  
12

  
13
public interface ResourceProvider extends Resource {
14

  
15
	public void notifyOpen() throws ResourceNotifyOpenException;
16

  
17
	public void notifyClose() throws ResourceNotifyCloseException;
18

  
19
	public void notifyDispose() throws ResourceNotifyDisposeException;
20

  
21
	public void notifyChanges() throws ResourceNotifyChangesException;
22

  
23
	public void prepare() throws PrepareResourceException;
24

  
25
	public boolean isThis(ResourceParameters parameters)
26
			throws ResourceException;
27
}
0 28

  
tags/v2_0_0_Build_2055/libraries/libFMap_dal/src/org/gvsig/fmap/dal/resource/spi/ResourceConsumer.java
1
package org.gvsig.fmap.dal.resource.spi;
2

  
3

  
4

  
5
public interface ResourceConsumer {
6

  
7
	public boolean closeResourceRequested(ResourceProvider resource);
8

  
9
	public void resourceChanged(ResourceProvider resource);
10

  
11
}
0 12

  
tags/v2_0_0_Build_2055/libraries/libFMap_dal/src/org/gvsig/fmap/dal/resource/spi/MultiResourceParameters.java
1
/* gvSIG. Geographic Information System of the Valencian Government
2
*
3
* Copyright (C) 2007-2008 Infrastructures and Transports Department
4
* of the Valencian Government (CIT)
5
* 
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU General Public License
8
* as published by the Free Software Foundation; either version 2
9
* of the License, or (at your option) any later version.
10
* 
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
* GNU General Public License for more details.
15
* 
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
19
* MA  02110-1301, USA.
20
* 
21
*/
22

  
23
/*
24
* AUTHORS (In addition to CIT):
25
* 2009 {}  {{Task}}
26
*/
27
package org.gvsig.fmap.dal.resource.spi;
28

  
29
import org.gvsig.fmap.dal.feature.spi.memory.MemoryResource;
30
import org.gvsig.tools.ToolsLocator;
31
import org.gvsig.tools.dynobject.DelegatedDynObject;
32
import org.gvsig.tools.dynobject.DynClass;
33
import org.gvsig.tools.dynobject.DynObjectManager;
34

  
35
/**
36
 * Parameters for the creation of a multi resource.
37
 * 
38
 * @author 2009- <a href="cordinyana@gvsig.org">C?sar Ordi?ana</a> - gvSIG team
39
 */
40
public class MultiResourceParameters extends AbstractResourceParameters {
41

  
42
	public static final String DYNCLASS_NAME = "MultiResourceParameters";
43

  
44
	private static final String FIELD_NAME = "name";
45

  
46
	private DelegatedDynObject delegatedDynObject;
47

  
48
	/**
49
	 * Creates a new {@link MultiResourceParameters}.
50
	 */
51
	public MultiResourceParameters() {
52
		this.delegatedDynObject =
53
				(DelegatedDynObject) ToolsLocator.getDynObjectManager()
54
						.createDynObject(registerDynClass());
55
	}
56

  
57
	/**
58
	 * Creates a new {@link MultiResourceParameters}.
59
	 * 
60
	 * @param name
61
	 *            for the parameters. Will be used as the name of the
62
	 *            {@link MemoryResource} created with this parametres.
63
	 */
64
	public MultiResourceParameters(String name) {
65
		this();
66
		setName(name);
67
	}
68

  
69
	protected DelegatedDynObject getDelegatedDynObject() {
70
		return delegatedDynObject;
71
	}
72

  
73
	public String getTypeName() {
74
		return MultiResource.TYPE_NAME;
75
	}
76

  
77
	public String getName() {
78
		return (String) getDelegatedDynObject().getDynValue(FIELD_NAME);
79
	}
80

  
81
	public void setName(String name) {
82
		getDelegatedDynObject().setDynValue(FIELD_NAME, name);
83
	}
84

  
85
	/**
86
	 * Registers the {@link DynClass} of this parameters attributes.
87
	 * 
88
	 * @return the dyn class of this parameters
89
	 */
90
	private DynClass registerDynClass() {
91
		DynObjectManager dynman = ToolsLocator.getDynObjectManager();
92
		DynClass dynClass = dynman.get(DYNCLASS_NAME);
93
		if (dynClass == null) {
94
			dynClass = dynman.add(DYNCLASS_NAME);
95
			dynClass.addDynFieldString(FIELD_NAME).setDescription(
96
					"The name of the multi resource");
97
		}
98
		return dynClass;
99
	}
100
}
tags/v2_0_0_Build_2055/libraries/libFMap_dal/src/org/gvsig/fmap/dal/resource/spi/AbstractResource.java
1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 */
22

  
23
/*
24
 * AUTHORS (In addition to CIT):
25
 * 2009 IVER T.I   {{Task}}
26
 */
27

  
28
/**
29
 *
30
 */
31
package org.gvsig.fmap.dal.resource.spi;
32

  
33
import java.lang.ref.WeakReference;
34
import java.util.ArrayList;
35
import java.util.Iterator;
36
import java.util.List;
37

  
38
import org.gvsig.fmap.dal.exception.CopyParametersException;
39
import org.gvsig.fmap.dal.exception.InitializeException;
40
import org.gvsig.fmap.dal.resource.Resource;
41
import org.gvsig.fmap.dal.resource.ResourceAction;
42
import org.gvsig.fmap.dal.resource.ResourceNotification;
43
import org.gvsig.fmap.dal.resource.ResourceParameters;
44
import org.gvsig.fmap.dal.resource.exception.AccessResourceException;
45
import org.gvsig.fmap.dal.resource.exception.PrepareResourceException;
46
import org.gvsig.fmap.dal.resource.exception.ResourceException;
47
import org.gvsig.fmap.dal.resource.exception.ResourceExecuteException;
48
import org.gvsig.fmap.dal.resource.exception.ResourceNotifyChangesException;
49
import org.gvsig.fmap.dal.resource.exception.ResourceNotifyCloseException;
50
import org.gvsig.fmap.dal.resource.exception.ResourceNotifyDisposeException;
51
import org.gvsig.fmap.dal.resource.exception.ResourceNotifyOpenException;
52
import org.gvsig.fmap.dal.resource.impl.DefaultResourceNotification;
53
import org.gvsig.tools.observer.Observer;
54
import org.gvsig.tools.observer.WeakReferencingObservable;
55
import org.gvsig.tools.observer.impl.BaseWeakReferencingObservable;
56
import org.gvsig.tools.observer.impl.DelegateWeakReferencingObservable;
57

  
58
/**
59
 * <p>
60
 * Base implementation for Resource
61
 * </p>
62
 * 
63
 * <p>
64
 * This implementation not define the {@link Resource#begin()} and
65
 * {@link Resource#end()}
66
 * </p>
67
 * 
68
 * @author jmvivo
69
 * 
70
 */
71
public abstract class AbstractResource implements ResourceProvider,
72
		WeakReferencingObservable {
73

  
74
	private DelegateWeakReferencingObservable delegateObservable;
75

  
76
	private List consumers;
77

  
78
	private long lastTimeOpen;
79
	private long lastTimeUsed;
80

  
81
	private ResourceParameters parameters;
82
	private ResourceParameters preparedParameters;
83

  
84
	private Object data;
85

  
86
	private int openCount;
87

  
88
	/**
89
	 * Number of times an execute is being simultaneously performed with this
90
	 * resource.
91
	 */
92
	private int executeCount = 0;
93

  
94
	protected final Object lock;
95

  
96
	protected AbstractResource(ResourceParameters parameters)
97
			throws InitializeException {
98
		consumers = new ArrayList();
99
		lastTimeOpen = System.currentTimeMillis();
100
		lastTimeUsed = lastTimeOpen;
101

  
102
		openCount = 0;
103

  
104
		preparedParameters = null;
105
		delegateObservable = new DelegateWeakReferencingObservable(this);
106
		lock = new Object();
107
		try {
108
			this.parameters = (ResourceParameters) parameters.getCopy();
109
		} catch (CopyParametersException e) {
110
			throw new InitializeException(e);
111
		}
112

  
113
	}
114

  
115
	protected final void updateLastTimeUsed() {
116
		lastTimeUsed = System.currentTimeMillis();
117
	}
118

  
119
	protected final void updateLastTimeOpen() {
120
		lastTimeOpen = System.currentTimeMillis();
121
		lastTimeUsed = lastTimeOpen;
122
	}
123

  
124
	public final long getLastTimeOpen() {
125
		return lastTimeOpen;
126
	}
127

  
128
	public final long getLastTimeUsed() {
129
		return lastTimeUsed;
130
	}
131

  
132
	public final ResourceParameters getParameters() {
133
		if (preparedParameters != null) {
134
			return preparedParameters;
135
		}
136
		return this.parameters;
137
	}
138

  
139
	public void prepare(ResourceParameters params)
140
			throws PrepareResourceException {
141
		ResourceNotification notification =
142
				new DefaultResourceNotification(this,
143
						ResourceNotification.PREPARE, params);
144
		this.delegateObservable.notifyObservers(notification);
145
	}
146

  
147
	public void prepare() throws PrepareResourceException {
148
		if (preparedParameters == null) {
149
			try {
150
				ResourceParameters params =
151
						(ResourceParameters) parameters.getCopy();
152
				prepare(params);
153
				preparedParameters = params;
154
			} catch (CopyParametersException e) {
155
				throw new PrepareResourceException(this, e);
156
			}
157
		}
158

  
159
	}
160

  
161
	public void notifyOpen() throws ResourceNotifyOpenException {
162
		this.notifyObserver(ResourceNotification.OPEN);
163
		updateLastTimeOpen();
164
		openCount++;
165
	}
166

  
167
	public void notifyClose() throws ResourceNotifyCloseException {
168
		if (openCount <= 0) {
169
			throw new IllegalStateException();
170
		}
171
		this.notifyObserver(ResourceNotification.CLOSE);
172
		openCount--;
173
	}
174

  
175
	public void notifyChanges() throws ResourceNotifyChangesException {
176
		this.notifyObserver(ResourceNotification.CHANGED);
177

  
178
		Iterator it = consumers.iterator();
179
		while (it.hasNext()) {
180
			ResourceConsumer consumer =
181
					(ResourceConsumer) ((WeakReference) it.next()).get();
182
			if (consumer != null) {
183
				consumer.resourceChanged(this);
184
			} else {
185
				it.remove();
186
			}
187
		}
188
	}
189

  
190
	public boolean isOpen() {
191
		return this.openCount > 0;
192
	}
193

  
194
	public int openCount() {
195
		return this.openCount;
196
	}
197

  
198
	public void addObserver(Observer o) {
199
		this.delegateObservable.addObserver(o);
200
	}
201

  
202
	public void deleteObserver(Observer o) {
203
		this.delegateObservable.deleteObserver(o);
204
	}
205

  
206
	public void deleteObservers() {
207
		this.delegateObservable.deleteObservers();
208

  
209
	}
210

  
211
	public final void addObservers(BaseWeakReferencingObservable observers) {
212
		this.delegateObservable.addObservers(observers);
213
	}
214

  
215
	public final void addConsumer(ResourceConsumer consumer) {
216
		this.updateConsumersList();
217
		consumers.add(new WeakReference(consumer));
218
	}
219

  
220
	public final void removeConsumer(ResourceConsumer consumer) {
221
		ResourceConsumer cur;
222
		Iterator it = consumers.iterator();
223
		while (it.hasNext()) {
224
			cur = (ResourceConsumer) ((WeakReference) it.next()).get();
225
			if (cur == null || (cur == consumer)) {
226
				it.remove();
227
			}
228
		}
229
	}
230

  
231
	public int getConsumersCount() {
232
		this.updateConsumersList();
233
		return consumers.size();
234
	}
235

  
236
	private synchronized void updateConsumersList() {
237
		Iterator it = consumers.iterator();
238
		WeakReference ref;
239
		while (it.hasNext()) {
240
			ref = (WeakReference) it.next();
241
			if (ref.get() == null) {
242
				it.remove();
243
			}
244
		}
245
	}
246

  
247
	public void closeRequest() throws ResourceException {
248
		if (inUse()) {
249
			return;
250
		}
251
		if (consumers != null) {
252
			for (int i = 0; i < consumers.size(); i++) {
253

  
254
			}
255
		}
256
		if (consumers != null) {
257
			Iterator it = consumers.iterator();
258
			while (it.hasNext()) {
259
				ResourceConsumer consumer =
260
						(ResourceConsumer) ((WeakReference) it.next()).get();
261
				if (consumer != null) {
262
					consumer.closeResourceRequested(this);
263
				} else {
264
					it.remove();
265
				}
266
			}
267
		}
268
	}
269

  
270
	public void setData(Object data) {
271
		this.data = data;
272
	}
273

  
274
	public Object getData() {
275
		return this.data;
276
	}
277

  
278
	protected void notifyObserver(String type) {
279
		if (delegateObservable != null) {
280
			this.delegateObservable.notifyObservers(new DefaultResourceNotification(
281
					this, type));
282
		}
283
	}
284

  
285
	public void notifyDispose() throws ResourceNotifyDisposeException {
286
		this.notifyObserver(ResourceNotification.DISPOSE);
287

  
288
		if (consumers != null) {
289
			consumers.clear();
290
		}
291

  
292
		lastTimeOpen = 0l;
293
		lastTimeUsed = 0l;
294

  
295
		data = null;
296

  
297
		if (delegateObservable != null) {
298
			delegateObservable.deleteObservers();
299
		}
300
	}
301

  
302
	public final boolean inUse() {
303
		return executeCount > 0;
304
	}
305

  
306
	public Object execute(ResourceAction action)
307
			throws ResourceExecuteException {
308
		Object value = null;
309
		synchronized (lock) {
310
			executeBegins();
311
			try {
312
				value = performExecution(action);
313
			} catch (Exception e) {
314
				throw new ResourceExecuteException(this, e);
315
			} finally {
316
				executeEnds();
317
			}
318
		}
319
		return value;
320
	}
321

  
322
	protected Object performExecution(ResourceAction action) throws Exception {
323
		return action.run();
324
	}
325

  
326
	protected final void executeBegins() {
327
		executeCount++;
328
	}
329

  
330
	protected final void executeEnds() {
331
		updateLastTimeUsed();
332
		executeCount--;
333
	}
334

  
335
	/**
336
	 * Returns the name of the {@link Resource}.
337
	 * 
338
	 * @throws AccessResourceException
339
	 *             if there is an error while accessing the resource
340
	 */
341
	public abstract String getName() throws AccessResourceException;
342

  
343
	/**
344
	 * Returns the real resource represented by this object.
345
	 * 
346
	 * @throws AccessResourceException
347
	 *             if there is an error while accessing the resource
348
	 */
349
	public abstract Object get() throws AccessResourceException;
350

  
351
}
0 352

  
tags/v2_0_0_Build_2055/libraries/libFMap_dal/src/org/gvsig/fmap/dal/resource/spi/AbstractNonBlockingResource.java
1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 */
22

  
23
/*
24
 * AUTHORS (In addition to CIT):
25
 * 2009 IVER T.I   {{Task}}
26
 */
27

  
28
/**
29
 *
30
 */
31
package org.gvsig.fmap.dal.resource.spi;
32

  
33
import org.gvsig.fmap.dal.exception.InitializeException;
34
import org.gvsig.fmap.dal.resource.ResourceAction;
35
import org.gvsig.fmap.dal.resource.ResourceParameters;
36
import org.gvsig.fmap.dal.resource.exception.ResourceExecuteException;
37

  
38
/**
39
 * <p>
40
 * Abstract Implementation for Resource that allows the concurrent access.
41
 * </p>
42
 * 
43
 * @author jmvivo
44
 * 
45
 */
46
public abstract class AbstractNonBlockingResource extends AbstractResource {
47

  
48
	/**
49
	 * @param parameters
50
	 * @throws InitializeException
51
	 */
52
	public AbstractNonBlockingResource(ResourceParameters parameters)
53
			throws InitializeException {
54
		super(parameters);
55
	}
56

  
57
	public Object execute(ResourceAction action)
58
			throws ResourceExecuteException {
59
		Object value = null;
60
		synchronized (lock) {
61
			executeBegins();
62
		}
63
		try {
64
			value = performExecution(action);
65
		} catch (Exception e) {
66
			throw new ResourceExecuteException(this, e);
67
		} finally {
68
			synchronized (lock) {
69
				executeEnds();
70
			}
71
		}
72
		return value;
73
	}
74
}
0 75

  
tags/v2_0_0_Build_2055/libraries/libFMap_dal/src/org/gvsig/fmap/dal/resource/spi/AbstractResourceParameters.java
1
package org.gvsig.fmap.dal.resource.spi;
2

  
3
import org.gvsig.fmap.dal.resource.ResourceParameters;
4
import org.gvsig.fmap.dal.spi.AbstractDataParameters;
5

  
6
public abstract class AbstractResourceParameters extends AbstractDataParameters
7
		implements ResourceParameters {
8

  
9

  
10
}
0 11

  
tags/v2_0_0_Build_2055/libraries/libFMap_dal/src/org/gvsig/fmap/dal/resource/spi/ResourceManagerProviderServices.java
1
package org.gvsig.fmap.dal.resource.spi;
2

  
3
import java.util.List;
4

  
5
import org.gvsig.fmap.dal.DataParameters;
6
import org.gvsig.fmap.dal.exception.DataException;
7
import org.gvsig.fmap.dal.exception.InitializeException;
8
import org.gvsig.fmap.dal.resource.Resource;
9
import org.gvsig.fmap.dal.resource.ResourceManager;
10
import org.gvsig.fmap.dal.resource.ResourceParameters;
11

  
12
public interface ResourceManagerProviderServices extends ResourceManager {
13

  
14
	public boolean register(String type, String description, Class handler,
15
			Class params);
16

  
17
	public DataParameters createParameters(String type)
18
			throws InitializeException;
19

  
20
	public ResourceProvider createAddResource(ResourceParameters params)
21
			throws InitializeException;
22

  
23
	public ResourceProvider createResource(ResourceParameters params)
24
			throws InitializeException;
25

  
26
	public ResourceProvider createAddResource(String type, Object[] params)
27
			throws InitializeException;
28

  
29
	public ResourceProvider createResource(String type, Object[] params)
30
			throws InitializeException;
31

  
32
	public void remove(Resource resource) throws DataException;
33

  
34
	public void remove(String name) throws DataException;
35
	
36
	public List getResourceProviders();
37

  
38
}
0 39

  
tags/v2_0_0_Build_2055/libraries/libFMap_dal/src/org/gvsig/fmap/dal/resource/ResourceNotification.java
1
/* gvSIG. Geographic Information System of the Valencian Government
2
*
3
* Copyright (C) 2007-2008 Infrastructures and Transports Department
4
* of the Valencian Government (CIT)
5
*
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU General Public License
8
* as published by the Free Software Foundation; either version 2
9
* of the License, or (at your option) any later version.
10
*
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
* GNU General Public License for more details.
15
*
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
* MA  02110-1301, USA.
20
*
21
*/
22

  
23
/*
24
* AUTHORS (In addition to CIT):
25
* 2008 IVER T.I   {{Task}}
26
*/
27

  
28
/**
29
 *
30
 */
31
package org.gvsig.fmap.dal.resource;
32

  
33

  
34
/**
35
 * A resource notification is related to a change in the state of a resource.
36
 * It is sent to all resource observers when appropriate. The notification contains
37
 * the type of change and access to the resource information.
38
 * 
39
 * This includes:
40
 * 
41
 * <ul>
42
 * 	<li>A resource has been opened</li>
43
 *  <li>A resource has been closed</li>
44
 *  <li>A resource has been prepared</li>
45
 *  <li>A resource property has been changed</li>
46
 *  <li>A resource is being disposed</li>
47
 *  <li>A resource is being opened</li>
48
 *  <li>A resource is being closed</li>
49
 * </ul>	
50
 * 
51
 * @author jmvivo
52
 */
53
public interface ResourceNotification {
54

  
55
	/** A resource has been opened */
56
	public static final String OPENED = "Opened_Resource";
57
	/** A resource has been closed */
58
	public static final String CLOSED = "Closed_Resource";
59
	/** A resource has been prepared */
60
	public static final String PREPARE = "Prepare_Resource";
61
	/** A resource property has been changed */
62
	public static final String CHANGED = "Changed_Resource";
63
	/** A resource is being disposed */
64
	public static final String DISPOSE = "Begin_Dispose_Resource";
65
	/** A resource is being opened */
66
	public static final String OPEN = "Begin_Open_Resource";
67
	/** A resource is being closed */
68
	public static final String CLOSE = "Begin_Close_Resource";
69

  
70
	/**
71
	 * Returns the parameters of the resource that caused
72
	 * this notification.
73
	 * 
74
	 * @return
75
	 * 		the parameters of this notification's source
76
	 */
77
	public ResourceParameters getParameters();
78
	
79
	/**
80
	 * Returns the resource that caused this notification.
81
	 * 
82
	 * @return
83
	 * 		this notification's source
84
	 */
85
	public Resource getResource();
86
	
87
	/**
88
	 * Returns the type of this notification.
89
	 * 
90
	 * @return
91
	 * 		this notification's type. For the allowed values see the 
92
	 * constants defined in this interface.
93
	 */
94
	public String getType();
95

  
96
}
97

  
0 98

  
tags/v2_0_0_Build_2055/libraries/libFMap_dal/src/org/gvsig/fmap/dal/resource/exception/DisposeResorceManagerException.java
1
package org.gvsig.fmap.dal.resource.exception;
2

  
3
import org.gvsig.fmap.dal.exception.DataListException;
4

  
5
public class DisposeResorceManagerException extends DataListException {
6

  
7
	/**
8
	 *
9
	 */
10
	private static final long serialVersionUID = -1818272776768733342L;
11

  
12
	private final static String MESSAGE_FORMAT = "Exception disposing.";
13
	private final static String MESSAGE_KEY = "_DisposeResorceManagerException";
14

  
15
	public DisposeResorceManagerException() {
16
		super(MESSAGE_FORMAT, MESSAGE_KEY, serialVersionUID);
17
	}
18
}
0 19

  
tags/v2_0_0_Build_2055/libraries/libFMap_dal/src/org/gvsig/fmap/dal/resource/exception/PrepareResourceException.java
1
package org.gvsig.fmap.dal.resource.exception;
2

  
3
import org.gvsig.fmap.dal.resource.Resource;
4

  
5
public class PrepareResourceException extends ResourceException {
6

  
7
	/**
8
	 *
9
	 */
10
	private static final long serialVersionUID = 2198478752691400900L;
11
	private final static String MESSAGE_FORMAT = "Exception preparing '%(resource)s'.";
12
	private final static String MESSAGE_KEY = "_PrepareResourceException";
13

  
14
	public PrepareResourceException(Resource resource, Throwable cause) {
15
		super(resource, MESSAGE_FORMAT, cause, MESSAGE_KEY, serialVersionUID);
16
	}
17

  
18
}
0 19

  
tags/v2_0_0_Build_2055/libraries/libFMap_dal/src/org/gvsig/fmap/dal/resource/exception/ResourceNotRegisteredException.java
1
package org.gvsig.fmap.dal.resource.exception;
2

  
3
import org.gvsig.fmap.dal.exception.InitializeException;
4

  
5
public class ResourceNotRegisteredException extends InitializeException {
6

  
7
	/**
8
	 *
9
	 */
10
	private static final long serialVersionUID = -3783563399272946137L;
11
	private final static String MESSAGE_FORMAT = "The '%(resourceType)' resource type is not registered.";
12
	private final static String MESSAGE_KEY = "_ResourceNotRegisteredException";
13

  
14
	public ResourceNotRegisteredException(String resourceType) {
15
		super(MESSAGE_FORMAT, MESSAGE_KEY, serialVersionUID);
16
		this.values.put("resourceType", resourceType);
17
	}
18
}
0 19

  
tags/v2_0_0_Build_2055/libraries/libFMap_dal/src/org/gvsig/fmap/dal/resource/exception/ResourceNotifyCloseException.java
1
package org.gvsig.fmap.dal.resource.exception;
2

  
3
import org.gvsig.fmap.dal.resource.Resource;
4

  
5
public class ResourceNotifyCloseException extends ResourceException {
6

  
7

  
8
	/**
9
	 *
10
	 */
11
	private static final long serialVersionUID = -805056352573291250L;
12
	private final static String MESSAGE_FORMAT = "Resource '%(resource)s' changed.";
13
	private final static String MESSAGE_KEY = "_ResourceNotifyCloseExceptionn";
14

  
15
	public ResourceNotifyCloseException(Resource resource) {
16
		super(resource, MESSAGE_FORMAT, MESSAGE_KEY, serialVersionUID);
17
	}
18
}
19

  
0 20

  
tags/v2_0_0_Build_2055/libraries/libFMap_dal/src/org/gvsig/fmap/dal/resource/exception/ResourceException.java
1
package org.gvsig.fmap.dal.resource.exception;
2

  
3
import java.util.HashMap;
4
import java.util.Map;
5

  
6
import org.gvsig.fmap.dal.exception.DataException;
7
import org.gvsig.fmap.dal.resource.Resource;
8

  
9
public abstract class ResourceException extends DataException {
10
	/**
11
	 *
12
	 */
13
	private static final long serialVersionUID = 1L;
14

  
15
	protected Map values = new HashMap();
16

  
17
	private Resource resource;
18

  
19
	public ResourceException(String messageFormat, Throwable cause,
20
			String messageKey, long code) {
21
		super(messageFormat, cause, messageKey, code);
22
	}
23

  
24
	public ResourceException(String messageFormat, String messageKey, long code) {
25
		super(messageFormat, messageKey, code);
26
	}
27

  
28
	public ResourceException(Resource resource, String messageFormat,
29
			Throwable cause, String messageKey, long code) {
30
		super(messageFormat, cause, messageKey, code);
31
		this.resource = resource;
32
		try {
33
			setValue("resource", resource.getName());
34
		} catch (AccessResourceException e) {
35
			setValue("resource", "unknow");
36
		}
37
	}
38

  
39
	public ResourceException(Resource resource, String messageFormat,
40
			String messageKey, long code) {
41
		super(messageFormat, messageKey, code);
42
		this.resource = resource;
43
		try {
44
			setValue("resource", resource.getName());
45
		} catch (AccessResourceException e) {
46
			setValue("resource", "unknow");
47
		}
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff