svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.api / src / main / java / org / gvsig / fmap / dal / resource / Resource.java @ 40435
History | View | Annotate | Download (4.52 KB)
1 | 40435 | jjdelcerro | 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 | } |