-
Notifications
You must be signed in to change notification settings - Fork 143
/
RarHandler.java
163 lines (143 loc) · 5.89 KB
/
RarHandler.java
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
/*
* Copyright (c) 2022, 2023 Contributors to the Eclipse Foundation
* Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package com.sun.enterprise.connectors.module;
import com.sun.appserv.connectors.internal.api.ConnectorRuntimeException;
import com.sun.appserv.connectors.internal.api.ConnectorsClassLoaderUtil;
import com.sun.appserv.connectors.internal.api.ConnectorsUtil;
import com.sun.enterprise.deploy.shared.AbstractArchiveHandler;
import com.sun.enterprise.security.ee.perms.PermsArchiveDelegate;
import com.sun.enterprise.security.ee.perms.SMGlobalPolicyUtil;
import com.sun.logging.LogDomains;
import jakarta.inject.Inject;
import jakarta.inject.Named;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.security.AccessController;
import java.security.PrivilegedActionException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.glassfish.api.deployment.DeploymentContext;
import org.glassfish.api.deployment.archive.ArchiveDetector;
import org.glassfish.api.deployment.archive.RarArchiveType;
import org.glassfish.api.deployment.archive.ReadableArchive;
import org.glassfish.loader.util.ASClassLoaderUtil;
import org.jvnet.hk2.annotations.Service;
/**
* Archive handler for resource-adapters
*
* @author Jagadish Ramu
*/
@Service(name = RarArchiveType.ARCHIVE_TYPE)
public class RarHandler extends AbstractArchiveHandler {
// This class should be moved to connector runtime along with ConnectorClassLoaderUtil.
// We should also consider merging connectors-connector with connectors-internal-api
@Inject
private ConnectorsClassLoaderUtil loader;
@Inject
@Named(RarArchiveType.ARCHIVE_TYPE)
private ArchiveDetector detector;
private final Logger _logger = LogDomains.getLogger(RarHandler.class, LogDomains.RSR_LOGGER);
/**
* {@inheritDoc}
*/
@Override
public String getArchiveType() {
return RarArchiveType.ARCHIVE_TYPE;
}
/**
* {@inheritDoc}
*/
@Override
public boolean handles(ReadableArchive archive) throws IOException {
return detector.handles(archive);
}
/**
* {@inheritDoc}
*/
@Override
public ClassLoader getClassLoader(ClassLoader parent, DeploymentContext context) {
try {
String moduleDir = context.getSource().getURI().getPath();
String moduleName = context.getSource().getName();
List<URI> appLibs = null;
try {
appLibs = context.getAppLibs();
if(_logger.isLoggable(Level.FINEST)){
_logger.log(Level.FINEST, "installed libraries (--applibs and EXTENSTION_LIST) for rar " +
"[ "+moduleName+" ] : " + appLibs);
}
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
ClassLoader carCL;
if (isEmbedded(context)) {
String applicationName = ConnectorsUtil.getApplicationName(context);
String embeddedRarName = ConnectorsUtil.getEmbeddedRarModuleName(applicationName, moduleName);
// ear's classloader hierarchy is : module-CL -> ear-CL (contains all ejb module classpath)
// -> embedded-RAR-CL -> ear-lib-CL.
// parent provided here is ear-CL, we need to use
// ear-lib-CL as parent for embedded-RAR module-CL
carCL = loader.createRARClassLoader(moduleDir, parent.getParent().getParent(), embeddedRarName, appLibs);
} else {
carCL = loader.createRARClassLoader(moduleDir, null, moduleName, appLibs);
}
try {
final DeploymentContext dc = context;
final ClassLoader cl = carCL;
AccessController.doPrivileged(
new PermsArchiveDelegate.SetPermissionsAction(SMGlobalPolicyUtil.CommponentType.rar, dc, cl));
} catch (PrivilegedActionException e) {
throw new SecurityException(e.getException());
}
return carCL;
} catch (ConnectorRuntimeException e) {
throw new RuntimeException(e);
}
}
/**
* indicates whether the .rar being deployed is standalone or embedded
* @param context deployment context
* @return boolean indicating whether its embedded .rar
*/
private boolean isEmbedded(DeploymentContext context) {
ReadableArchive archive = context.getSource();
return (archive != null && archive.getParentArchive() != null);
}
/**
* Returns the classpath URIs for this archive.
*
* @param archive file
* @return classpath URIs for this archive
*/
@Override
public List<URI> getClassPathURIs(ReadableArchive archive) {
List<URI> uris = super.getClassPathURIs(archive);
try {
File archiveFile = new File(archive.getURI());
if (archiveFile.exists() && archiveFile.isDirectory()) {
// add top level jars
uris.addAll(ASClassLoaderUtil.getLibDirectoryJarURIs(archiveFile));
}
} catch (Exception e) {
_logger.log(Level.WARNING, e.getMessage(), e);
}
return uris;
}
}