0
# Resource Management
1
2
Unified resource management API supporting various resource types with automatic cleanup and efficient access patterns for files, memory, JARs, and URLs.
3
4
## Capabilities
5
6
### Resource Interface
7
8
Abstract resource representation providing unified access to different resource types.
9
10
```java { .api }
11
/**
12
* Abstract resource interface
13
*/
14
public interface Resource {
15
/** Check if resource exists */
16
boolean exists();
17
18
/** Check if resource is directory */
19
boolean isDirectory();
20
21
/** Check if resource is file */
22
boolean isFile();
23
24
/** Get resource path */
25
Path getPath();
26
27
/** Get resource URI */
28
URI getURI();
29
30
/** Resolve sub-resource */
31
Resource resolve(String subUriPath);
32
33
/** Get input stream */
34
InputStream newInputStream() throws IOException;
35
36
/** Get file name */
37
String getFileName();
38
39
/** Get last modified time */
40
Instant lastModified();
41
42
/** Get resource length */
43
long length();
44
45
/** List directory contents */
46
List<Resource> list();
47
}
48
```
49
50
### ResourceFactory Interface
51
52
Factory for creating and managing resources.
53
54
```java { .api }
55
/**
56
* Resource factory interface
57
*/
58
public interface ResourceFactory {
59
/** Create resource from URI or path string */
60
Resource newResource(String uriOrPath);
61
62
/** Create resource from Path */
63
Resource newResource(Path path);
64
65
/** Create resource from URI */
66
Resource newResource(URI uri);
67
68
/** Closeable resource factory */
69
interface Closeable extends ResourceFactory, java.io.Closeable {
70
// Factory that can be closed to release resources
71
}
72
73
/** Lifecycle-managed resource factory */
74
interface LifeCycle extends ResourceFactory, org.eclipse.jetty.util.component.LifeCycle {
75
// Factory with full lifecycle management
76
}
77
}
78
```
79
80
### Resource Implementations
81
82
```java { .api }
83
/**
84
* File system path-based resource
85
*/
86
public class PathResource implements Resource {
87
/** Create path resource */
88
public PathResource(Path path);
89
90
/** Create path resource from file */
91
public PathResource(File file);
92
}
93
94
/**
95
* In-memory resource implementation
96
*/
97
public class MemoryResource implements Resource {
98
/** Create memory resource from byte array */
99
public MemoryResource(byte[] data);
100
101
/** Create memory resource with name and data */
102
public MemoryResource(String name, byte[] data);
103
}
104
105
/**
106
* Combined resource from multiple sources
107
*/
108
public class CombinedResource implements Resource {
109
/** Create combined resource */
110
public CombinedResource(Resource... resources);
111
}
112
```
113
114
### ResourceFactory Implementations
115
116
```java { .api }
117
/**
118
* Path-based resource factory
119
*/
120
public class PathResourceFactory implements ResourceFactory {
121
/** Create factory */
122
public PathResourceFactory();
123
}
124
125
/**
126
* URL-based resource factory
127
*/
128
public class URLResourceFactory implements ResourceFactory {
129
/** Create factory */
130
public URLResourceFactory();
131
}
132
```
133
134
**Usage Examples:**
135
136
```java
137
import org.eclipse.jetty.util.resource.*;
138
139
// Create resource factory
140
ResourceFactory factory = new PathResourceFactory();
141
142
// Create resources
143
Resource configFile = factory.newResource("/app/config/app.properties");
144
Resource webRoot = factory.newResource("/var/www/html");
145
146
// Check resource properties
147
if (configFile.exists() && configFile.isFile()) {
148
try (InputStream input = configFile.newInputStream()) {
149
Properties props = new Properties();
150
props.load(input);
151
}
152
}
153
154
// List directory contents
155
if (webRoot.isDirectory()) {
156
for (Resource file : webRoot.list()) {
157
System.out.println(file.getFileName() + " - " + file.length() + " bytes");
158
}
159
}
160
161
// Resolve sub-resources
162
Resource indexFile = webRoot.resolve("index.html");
163
Resource cssDir = webRoot.resolve("css/");
164
```
165
166
## Error Handling
167
168
Resource operations handle errors through:
169
170
- `IOException` for I/O operations that fail
171
- `IllegalArgumentException` for invalid paths or URIs
172
- Graceful handling of missing resources (exists() returns false)
173
- Automatic resource cleanup when using try-with-resources