下面列出了org.apache.http.util.Args#notNull ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Delete oArtifact from metadata.xml
* @param oArtifact {@link OArtifact} for delete from metadata.xml
* @throws IllegalArgumentException if oArtifact is null
*/
@SuppressWarnings("unchecked")
void delete(OArtifact oArtifact) {
Args.notNull(oArtifact, "oArtifact");
Document document = readDocumentFromFile(pathToMetadata);
if (document == null) documentCannotReadException(pathToMetadata);
NodeList nodeList = executeExpression(ALL_MODULES_EXP, document);
if (nodeList != null) {
Element root = document.getDocumentElement();
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) node;
Element dependencyElement = (Element) element.getElementsByTagName(MetadataTag.DEPENDENCY.get()).item(0);
if (isNecessaryElement(dependencyElement, oArtifact)) {
root.removeChild(element);
break;
}
}
}
saveDocument(document, pathToMetadata);
}
}
/**
* Search pom.xml if jar file
* @param path {@link Path} of jar file
* @return {@link Path} of pom.xml or Optional.absent() if pom.xml is not present
* @throws IllegalArgumentException if path is null
*/
public Path getPomFromJar(Path path) {
Args.notNull(path, "path");
Path result = null;
try {
JarFile jarFile = new JarFile(path.toFile());
Enumeration<JarEntry> entries = jarFile.entries();
while (entries.hasMoreElements()) {
JarEntry jarEntry = entries.nextElement();
if (jarEntry.getName().endsWith("pom.xml")) {
result = unarchivePomFile(jarFile, jarEntry);
break;
}
}
jarFile.close();
} catch (IOException e) {
if (LOG.isDebugEnabled()) {
LOG.error("Cannot open file to read. File: " + path.toAbsolutePath(), e);
}
}
return result;
}
private static void testArtifact(OArtifact expected, OArtifact actual) {
Args.notNull(expected, "expected");
Args.notNull(actual, "actual");
assertEquals("test artifact load", expected.isLoad(), actual.isLoad());
assertEquals("test artifact trusted", expected.isTrusted(), actual.isTrusted());
assertEquals("test artifact groupId", expected.getArtifactReference().getGroupId(),
actual.getArtifactReference().getGroupId());
assertEquals("test artifact artifactId", expected.getArtifactReference().getArtifactId(),
actual.getArtifactReference().getArtifactId());
assertEquals("test artifact version", expected.getArtifactReference().getVersion(),
actual.getArtifactReference().getVersion());
assertEquals("test artifact jar files", expected.getArtifactReference().getFile().getAbsoluteFile(),
actual.getArtifactReference().getFile().getAbsoluteFile());
assertEquals("test artifact descriptions", expected.getArtifactReference().getDescription(),
actual.getArtifactReference().getDescription());
}
/**
* Resolve artifacts. Download its dependencies
* @param artifacts {@link Set} for resolve
* @return {@link List} resolved dependencies of artifacts
* @throws IllegalArgumentException if artifacts is null
*/
public static List<Artifact> resolveArtifacts(List<Artifact> artifacts) {
Args.notNull(artifacts, "artifacts");
List<Artifact> result = Lists.newArrayList();
for (Artifact artifact : artifacts) {
result.addAll(resolveAndGetArtifactDependencies(artifact));
}
return result;
}
/**
* Execute XPath expression
* @param expression - {@link String} expression for execute
* @param document {@link Document} in which expression will execute
* @return {@link NodeList} with result or null if can't execute expression.
* @throws IllegalArgumentException if expression or document is null
*/
protected final NodeList executeExpression(String expression, Document document) {
Args.notNull(expression, "expression");
Args.notNull(document, "document");
XPath xPath = XPathFactory.newInstance().newXPath();
try {
return (NodeList) xPath.compile(expression).evaluate(document, XPathConstants.NODESET);
} catch (XPathExpressionException e) {
LOG.warn("Can't execute search query: {},", expression , e);
}
return null;
}
/**
* Update metadata.xml.
* oArtifacts will be write to metadata.xml or will be update its flag load or trusted.
* @param oArtifacts list of {@link OArtifact} for update
* @param updateJar true - jar will be update
* false - jar will not be update
* @throws IllegalArgumentException if oArtifacts is null
*/
@SuppressWarnings("unchecked")
void update(List<OArtifact> oArtifacts, boolean updateJar) {
Args.notNull(oArtifacts, "oArtifacts");
Document document = readDocumentFromFile(pathToMetadata);
if (document == null) documentCannotReadException(pathToMetadata);
NodeList nodeList = executeExpression(ALL_MODULES_EXP, document);
List<OArtifact> updatedModules = Lists.newArrayList();
if (nodeList != null) {
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
if (node.getNodeType() == Element.ELEMENT_NODE) {
Element element = (Element) node;
Element dependencyElement = (Element) element.getElementsByTagName(MetadataTag.DEPENDENCY.get()).item(0);
OArtifact oArtifact = containsInModulesConfigsList(dependencyElement, oArtifacts);
if (oArtifact != null) {
if (updateJar) {
changeoArtifactsLoadAndJar(element, oArtifact);
} else changeArtifactElement(element, oArtifact);
updatedModules.add(oArtifact);
}
}
}
}
if (updatedModules.size() != oArtifacts.size()) {
addArtifacts(difference(updatedModules, oArtifacts), document);
}
saveDocument(document, pathToMetadata);
}
/**
* Download artifacts
* @param artifacts {@link Set<Artifact>} artifacts for download
* @return {@link List<Artifact>} of resolved artifact
* @throws IllegalArgumentException if artifacts is null
*/
public List<Artifact> downloadArtifacts(Set<Artifact> artifacts) {
Args.notNull(artifacts, "artifacts");
Set<ArtifactRequest> artifactRequests = createArtifactRequests(artifacts);
List<ArtifactResult> artifactResults = resolveArtifactRequests(artifactRequests);
List<Artifact> result = Lists.newArrayList();
for (ArtifactResult res : artifactResults) {
result.add(res.getArtifact());
}
return result;
}
@Override
public HttpRoute determineRoute(final HttpHost host, final HttpRequest request, final HttpContext context) throws HttpException {
Args.notNull(request, "Request");
if (host == null) {
throw new ProtocolException("Target host is not specified");
}
final HttpClientContext clientContext = HttpClientContext.adapt(context);
final RequestConfig config = clientContext.getRequestConfig();
int remotePort;
if (host.getPort() <= 0) {
try {
remotePort = schemePortResolver.resolve(host);
} catch (final UnsupportedSchemeException e) {
throw new HttpException(e.getMessage());
}
} else
remotePort = host.getPort();
final Tuple<Inet4Address, Inet6Address> remoteAddresses = IpAddressTools.getRandomAddressesFromHost(host);
final Tuple<InetAddress, InetAddress> addresses = determineAddressPair(remoteAddresses);
final HttpHost target = new HttpHost(addresses.r, host.getHostName(), remotePort, host.getSchemeName());
final HttpHost proxy = config.getProxy();
final boolean secure = target.getSchemeName().equalsIgnoreCase("https");
clientContext.setAttribute(CHOSEN_IP_ATTRIBUTE, addresses.l);
log.debug("Setting route context attribute to {}", addresses.l);
if (proxy == null) {
return new HttpRoute(target, addresses.l, secure);
} else {
return new HttpRoute(target, addresses.l, proxy, secure);
}
}
@Substitute
@Override
public AuthScheme get(final HttpHost host) {
Args.notNull(host, "HTTP host");
return this.map.get(getKey(host));
}
public MultipartEntityBuilder addPart(String name, ContentBody contentBody) {
Args.notNull(name, "Name");
Args.notNull(contentBody, "Content body");
return addPart(FormBodyPartBuilder.create(name, contentBody).build());
}
public void start() {
Args.notNull(connMgr, "Connection manager");
thread.start();
}
public static <T> T notNull(String name, T object) {
return Args.notNull(object, name);
}
public NIdleConnectionEvictor setConnMgr(final NHttpClientConnectionManager connMgr){
this.connMgr = Args.notNull(connMgr, "Connection manager");
return this;
}
/**
* Constructor
* @param pathToMetadata {@link Path} of metadata.xml
* @throws IllegalArgumentException if pathToMetadata is null
*/
OMetadataReader(Path pathToMetadata) {
Args.notNull(pathToMetadata, "pathToMetadata");
this.pathToMetadata = pathToMetadata;
}
/**
* Search pom.xml in jar file
* @param pathToJar {@link Path} of jar file
* @return {@link Path} of pom.xml or Optional.absent() if pom.xml is not present in jar file
* @throws IllegalArgumentException if pathToJar is null
*/
public static Path getPomFromJar(Path pathToJar) {
Args.notNull(pathToJar, "pathToJar");
return jarUtils.getPomFromJar(pathToJar);
}
/**
* Update jar for oArtifact in metadata.xml
* @param oArtifact {@link OArtifact} for update
* @param updateJar true - jar will be update
* false - jar will not be update
* @throws IllegalArgumentException if oArtifact is null
*/
void update(OArtifact oArtifact, boolean updateJar) {
Args.notNull(oArtifact, "oArtifact");
update(Lists.newArrayList(oArtifact), updateJar);
}
/**
* Download artifacts
* @param artifacts {@link Set} for download
* @return {@link List} of downloaded artifacts
* @throws IllegalArgumentException if artifacts is null
*/
public static List<Artifact> downloadArtifacts(Set<Artifact> artifacts) {
Args.notNull(artifacts, "artifacts");
return aetherUtils.downloadArtifacts(artifacts);
}
/**
* Update or create oArtifacts in metadata.xml
* @param oArtifacts {@link List} for update or create in metadata.xml
* @throws IllegalArgumentException if oArtifacts is null
*/
public static void updateOArtifactsInMetadata(List<OArtifact> oArtifacts) {
Args.notNull(oArtifacts, "oArtifacts");
metadataUtil.updateOArtifactsMetadata(oArtifacts);
}
/**
* Create {@link OArtifact} from jar file
* @param pathToJar {@link Path} of jar file
* @return {@link OArtifact} of artifact or Optional.absent() if artifact is not present in jar file
* @throws IllegalArgumentException if pathToJar is null
*/
public static OArtifact getOArtifactFromJar(Path pathToJar) {
Args.notNull(pathToJar, "pathToJar");
return MavenResolver.get().getOArtifact(pathToJar);
}
/**
* Update metadata.xml
* @param oArtifact - {@link OArtifact} for update in metadata.xml
* @throws IllegalArgumentException if oArtifact is null
*/
void update(OArtifact oArtifact) {
Args.notNull(oArtifact, "oArtifact");
update(oArtifact, false);
}