Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (c) 1997, 2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0, which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
package com.sun.xml.wss.provider.wsit;
import com.sun.xml.wss.provider.wsit.logging.LogDomainConstants;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import jakarta.security.auth.message.config.AuthConfigFactory.RegistrationContext;
/**
* Used by GFServerConfigProvider to parse the configuration file. If
* a file does not exist originally, the default providers are not used.
* A file is only created if needed, which happens if providers are
* registered or unregistered through the store() or delete() methods.
*
* @author Bobby Bissett
*/
public final class RegStoreFileParser {
private static Logger logger =Logger.getLogger(
LogDomainConstants.WSIT_PVD_DOMAIN,
LogDomainConstants.WSIT_PVD_DOMAIN_BUNDLE);
private static final String SEP = ":";
private static final String CON_ENTRY = "con-entry";
private static final String REG_ENTRY = "reg-entry";
private static final String REG_CTX = "reg-ctx";
private static final String LAYER = "layer";
private static final String APP_CTX = "app-ctx";
private static final String DESCRIPTION = "description";
private static final String [] INDENT = { "", " ", " " };
private File confFile;
private List entries;
/*
* Loads the configuration file from the given filename.
* If a file is not found, then the default entries
* stored in GFAuthConfigFactory are used. Otherwise,
* the file is parsed to load the entries.
*
* The boolean argument tells whether to create the config
* file always (true) or only if it's needed (false).
*/
RegStoreFileParser(String pathParent, String pathChild, boolean create) {
try {
confFile = new File(pathParent, pathChild);
if (confFile.exists()) {
loadEntries();
} else {
if (create) {
synchronized (confFile) {
entries = JMACAuthConfigFactory.getDefaultProviders();
writeEntries();
}
} else {
if (logger.isLoggable(Level.FINER)) {
logger.log(Level.FINER, "jmac.factory_file_not_found",
pathParent + File.pathSeparator + pathChild);
}
}
}
} catch (IOException | IllegalArgumentException ioe) {
logWarningDefault(ioe);
}
// file not parsed
if (entries == null) {
entries = JMACAuthConfigFactory.getDefaultProviders();
}
}
private void logWarningUpdated(Exception exception) {
if (logger.isLoggable(Level.WARNING)) {
logger.log(Level.WARNING,
"jmac.factory_could_not_persist", exception.toString());
}
}
private void logWarningDefault(Exception exception) {
if (logger.isLoggable(Level.WARNING)) {
logger.log(Level.WARNING,
"jmac.factory_could_not_read", exception.toString());
}
}
/*
* Returns the in-memory list of entries.
*/
List getPersistedEntries() {
return entries;
}
/*
* Adds the provider to the entry list if it is not already
* present, creates the configuration file if necessary, and
* writes the entries to the file.
*/
void store(String className, RegistrationContext ctx, Map properties) {
synchronized (this) {
if (checkAndAddToList(className, ctx, properties)) {
try {
writeEntries();
} catch (IOException ioe) {
logWarningUpdated(ioe);
}
}
}
}
/*
* Removes the provider from the entry list if it is already
* present, creates the configuration file if necessary, and
* writes the entries to the file.
*/
void delete(RegistrationContext ctx) {
synchronized (this) {
if (checkAndRemoveFromList(ctx)) {
try {
writeEntries();
} catch (IOException ioe) {
logWarningUpdated(ioe);
}
}
}
}
/*
* If this entry does not exist, this method stores it in
* the entries list and returns true to indicate that the
* configuration file should be written.
*/
private boolean checkAndAddToList(String className,
RegistrationContext ctx, Map props) {
// convention is to use null for empty properties
if (props != null && props.isEmpty()) {
props = null;
}
EntryInfo newEntry = new EntryInfo(className, props, ctx);
EntryInfo entry = getMatchingRegEntry(newEntry);
// there is no matching entry, so add to list
if (entry == null) {
entries.add(newEntry);
return true;
}
// if constructor entry, don't need to check reg context
if (entry.isConstructorEntry()) {
return false;
}
// otherwise, check reg contexts to see if there is a match
if (entry.getRegContexts().contains(ctx)) {
return false;
}
// no matching context in existing entry, so add to existing entry
entry.getRegContexts().add(new RegistrationContextImpl(ctx));
return true;
}
/*
* If this registration context does not exist, this method
* returns false. Otherwise it removes the entry and returns
* true to indicate that the configuration file should be written.
*
* This only makes sense for registry entries.
*/
private boolean checkAndRemoveFromList(RegistrationContext target) {
boolean retValue = false;
for (EntryInfo info : entries) {
if (info.isConstructorEntry()) {
continue;
}
Iterator iter =
info.getRegContexts().iterator();
while (iter.hasNext()) {
RegistrationContext ctx = iter.next();
if (ctx.equals(target)) {
iter.remove();
retValue = true;
}
}
}
return retValue;
}
/*
* Used to find a matching registration entry in the 'entries'
* list without including registration contexts. If there is not
* a matching entry, return null.
*/
private EntryInfo getMatchingRegEntry(EntryInfo target) {
for (EntryInfo info : entries) {
if (info.equals(target)) {
return info;
}
}
return null;
}
/*
* This method overwrites the existing file with the
* current entries.
*/
private void writeEntries() throws IOException {
if (!confFile.canWrite() && logger.isLoggable(Level.WARNING)) {
logger.log(Level.WARNING, "jmac.factory_cannot_write_file",
confFile.getPath());
}
clearExistingFile();
PrintWriter out = new PrintWriter(confFile);
int indent = 0;
for (EntryInfo info : entries) {
if (info.isConstructorEntry()) {
writeConEntry(info, out, indent);
} else {
writeRegEntry(info, out, indent);
}
}
out.close();
}
/*
* Writes constructor entry output of the form:
*
* The first appearance of a colon ":" separates
* the key and value of the property (so a value may
* contain a colon as part of the string). For instance:
* "mydir:c:foo" would have key "mydir" and value "c:foo".
*/
private void writeConEntry(EntryInfo info, PrintWriter out, int i) {
out.println(INDENT[i++] + CON_ENTRY + " {");
out.println(INDENT[i] + info.getClassName());
Map props = info.getProperties();
if (props != null) {
for (String key : props.keySet()) {
out.println(INDENT[i] + key + SEP + props.get(key));
}
}
out.println(INDENT[--i] + "}");
}
/*
* Write registration entry output of the form:
*