[507] | 1 | package gov.hhs.fha.nhinc.connectmgr.uddi;
|
---|
| 2 |
|
---|
| 3 | import gov.hhs.fha.nhinc.common.connectionmanagerinfo.SuccessOrFailType;
|
---|
| 4 | import gov.hhs.fha.nhinc.common.connectionmanagerinfo.UDDIUpdateManagerForceRefreshRequestType;
|
---|
| 5 | import gov.hhs.fha.nhinc.common.connectionmanagerinfo.UDDIUpdateManagerForceRefreshResponseType;
|
---|
| 6 | import gov.hhs.fha.nhinc.connectmgr.data.CMBusinessEntities;
|
---|
| 7 | import gov.hhs.fha.nhinc.connectmgr.data.CMUDDIConnectionInfo;
|
---|
| 8 | import gov.hhs.fha.nhinc.connectmgr.data.CMUDDIConnectionInfoXML;
|
---|
| 9 |
|
---|
| 10 | import gov.hhs.fha.nhinc.properties.PropertyAccessor;
|
---|
| 11 | import java.io.File;
|
---|
| 12 | import java.io.FileWriter;
|
---|
| 13 | import java.text.SimpleDateFormat;
|
---|
| 14 | import java.util.Calendar;
|
---|
| 15 | import org.apache.commons.logging.Log;
|
---|
| 16 | import org.apache.commons.logging.LogFactory;
|
---|
| 17 |
|
---|
| 18 | /**
|
---|
| 19 | * Helper class for the web service.
|
---|
| 20 | *
|
---|
| 21 | * @author Les Westberg
|
---|
| 22 | */
|
---|
| 23 | public class UDDIUpdateManagerHelper
|
---|
| 24 | {
|
---|
| 25 | private static Log log = LogFactory.getLog(UDDIUpdateManagerHelper.class);
|
---|
| 26 | private static final String GATEWAY_PROPERTY_FILE = "gateway";
|
---|
| 27 | private static final String UDDI_REFRESH_KEEP_BACKUPS_PROPERTY = "UDDIRefreshKeepBackups";
|
---|
| 28 | private static final String UDDI_CONNECTION_INFO_FILENAME = "uddiConnectionInfo.xml";
|
---|
| 29 |
|
---|
| 30 | /**
|
---|
| 31 | * This method retrieves the data from the UDDI server and transforms it into
|
---|
| 32 | * and XML string and returns it.
|
---|
| 33 | *
|
---|
| 34 | * @return The XML representation of the data retrieved from UDDI.
|
---|
| 35 | */
|
---|
| 36 | private static String retrieveDataFromUDDI()
|
---|
| 37 | throws UDDIAccessorException
|
---|
| 38 | {
|
---|
| 39 | String sXML = "";
|
---|
| 40 |
|
---|
| 41 | UDDIAccessor oUDDIAccessor = new UDDIAccessor();
|
---|
| 42 | CMBusinessEntities oEntities = oUDDIAccessor.retrieveFromUDDIServer();
|
---|
| 43 |
|
---|
| 44 | if ((oEntities != null) &&
|
---|
| 45 | (oEntities.getBusinessEntity() != null) &&
|
---|
| 46 | (oEntities.getBusinessEntity().size() > 0))
|
---|
| 47 | {
|
---|
| 48 | CMUDDIConnectionInfo oUDDIConnectionInfo = new CMUDDIConnectionInfo();
|
---|
| 49 | oUDDIConnectionInfo.setBusinessEntities(oEntities);
|
---|
| 50 | try
|
---|
| 51 | {
|
---|
| 52 | sXML = CMUDDIConnectionInfoXML.serialize(oUDDIConnectionInfo);
|
---|
| 53 | }
|
---|
| 54 | catch (Exception e)
|
---|
| 55 | {
|
---|
| 56 | String sErrorMessage = "Failed to serialize information from UDDI into valid XML message. Error: " +
|
---|
| 57 | e.getMessage();
|
---|
| 58 | log.error(sErrorMessage, e);
|
---|
| 59 | throw new UDDIAccessorException(sErrorMessage, e);
|
---|
| 60 | }
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | return sXML;
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | /**
|
---|
| 67 | * This method is called to force a refresh of the uddiConnectionInfo.xml file.
|
---|
| 68 | * It will retrieve the data from the UDDI server and update the local XML file.
|
---|
| 69 | *
|
---|
| 70 | * @throws UDDIAccessorException
|
---|
| 71 | */
|
---|
| 72 | public static void forceRefreshUDDIFile()
|
---|
| 73 | throws UDDIAccessorException
|
---|
| 74 | {
|
---|
| 75 | if (log.isDebugEnabled())
|
---|
| 76 | {
|
---|
| 77 | log.debug("Start: UDDIUpdateManagerHelper.forceRefreshUDDIFile method - loading from UDDI server.");
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | String sXML = "";
|
---|
| 81 | Boolean bCreateBackups = true;
|
---|
| 82 | String sXMLFileLocation = "";
|
---|
| 83 |
|
---|
| 84 | try
|
---|
| 85 | {
|
---|
| 86 | sXML = retrieveDataFromUDDI();
|
---|
| 87 | }
|
---|
| 88 | catch (Exception e)
|
---|
| 89 | {
|
---|
| 90 | String sErrorMessage = "Failed to retrieve data from UDDI. Error: " + e.getMessage();
|
---|
| 91 | log.error(sErrorMessage, e);
|
---|
| 92 | throw new UDDIAccessorException(sErrorMessage, e);
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | if ((sXML != null) && (sXML.length() > 0))
|
---|
| 96 | {
|
---|
| 97 | try
|
---|
| 98 | {
|
---|
| 99 | bCreateBackups = PropertyAccessor.getPropertyBoolean(GATEWAY_PROPERTY_FILE, UDDI_REFRESH_KEEP_BACKUPS_PROPERTY);
|
---|
| 100 | }
|
---|
| 101 | catch (Exception e)
|
---|
| 102 | {
|
---|
| 103 | bCreateBackups = true;
|
---|
| 104 | String sErrorMessage = "Failed to retrieve property: " + UDDI_REFRESH_KEEP_BACKUPS_PROPERTY +
|
---|
| 105 | " from " + GATEWAY_PROPERTY_FILE + ".properties. Defaulting to creating backups. " +
|
---|
| 106 | "Error: " + e.getMessage();
|
---|
| 107 | log.warn(sErrorMessage, e);
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | sXMLFileLocation = PropertyAccessor.getPropertyFileLocation() + UDDI_CONNECTION_INFO_FILENAME;
|
---|
| 111 | File fCurrentFile = new File(sXMLFileLocation);
|
---|
| 112 |
|
---|
| 113 | Calendar oNow = Calendar.getInstance();
|
---|
| 114 | SimpleDateFormat oFormat = new SimpleDateFormat("MM-dd-yyyy_HH-mm-ss");
|
---|
| 115 | String sXMLFileLocationNewName = sXMLFileLocation + "." + oFormat.format(oNow.getTime());
|
---|
| 116 | File fNewFileName = new File(sXMLFileLocationNewName);
|
---|
| 117 |
|
---|
| 118 | if (fCurrentFile.exists())
|
---|
| 119 | {
|
---|
| 120 |
|
---|
| 121 | try
|
---|
| 122 | {
|
---|
| 123 | fCurrentFile.renameTo(fNewFileName);
|
---|
| 124 | }
|
---|
| 125 | catch (Exception e)
|
---|
| 126 | {
|
---|
| 127 | String sErrorMessage = "Failed to rename the current file: " + sXMLFileLocation +
|
---|
| 128 | " to: " + sXMLFileLocationNewName + ". Aborting changes to " +
|
---|
| 129 | "uddi connection information. Error: " + e.getMessage();
|
---|
| 130 | log.error(sErrorMessage, e);
|
---|
| 131 | throw new UDDIAccessorException(sErrorMessage, e);
|
---|
| 132 | }
|
---|
| 133 | } // if (fCurrentFile.exists())
|
---|
| 134 |
|
---|
| 135 | // Create the file with the new information.
|
---|
| 136 | //-------------------------------------------
|
---|
| 137 | FileWriter fwCurrentFile = null;
|
---|
| 138 | try
|
---|
| 139 | {
|
---|
| 140 | fwCurrentFile = new FileWriter(fCurrentFile);
|
---|
| 141 | fwCurrentFile.write(sXML);
|
---|
| 142 | }
|
---|
| 143 | catch (Exception e)
|
---|
| 144 | {
|
---|
| 145 | String sErrorMessage = "Failed to write file: " + sXMLFileLocation +
|
---|
| 146 | ". Error: " + e.getMessage();
|
---|
| 147 | log.error(sErrorMessage, e);
|
---|
| 148 | throw new UDDIAccessorException(sErrorMessage, e);
|
---|
| 149 | }
|
---|
| 150 | finally
|
---|
| 151 | {
|
---|
| 152 | if (fwCurrentFile != null)
|
---|
| 153 | {
|
---|
| 154 | try
|
---|
| 155 | {
|
---|
| 156 | fwCurrentFile.close();
|
---|
| 157 | }
|
---|
| 158 | catch (Exception e)
|
---|
| 159 | {
|
---|
| 160 | String sErrorMessage = "Failed to close file: " + sXMLFileLocation +
|
---|
| 161 | ". Error: " + e.getMessage();
|
---|
| 162 | log.error(sErrorMessage, e);
|
---|
| 163 | throw new UDDIAccessorException(sErrorMessage, e);
|
---|
| 164 | }
|
---|
| 165 | }
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | // if we are not creating backups... Then we need to delete our temproary one.
|
---|
| 169 | //------------------------------------------------------------------------------
|
---|
| 170 | if (!bCreateBackups)
|
---|
| 171 | {
|
---|
| 172 | try
|
---|
| 173 | {
|
---|
| 174 | fNewFileName.delete();
|
---|
| 175 | }
|
---|
| 176 | catch (Exception e)
|
---|
| 177 | {
|
---|
| 178 | String sErrorMessage = "Failed to delete the temporary backup file: " + sXMLFileLocationNewName +
|
---|
| 179 | "Error: " + e.getMessage();
|
---|
| 180 | log.error(sErrorMessage, e);
|
---|
| 181 | throw new UDDIAccessorException(sErrorMessage, e);
|
---|
| 182 | }
|
---|
| 183 | }
|
---|
| 184 | } // if ((sXML != null) && (sXML.length() > 0))
|
---|
| 185 |
|
---|
| 186 | if (log.isDebugEnabled())
|
---|
| 187 | {
|
---|
| 188 | log.debug("Done: UDDIUpdateManagerHelper.forceRefreshUDDIFile method - loading from UDDI server.");
|
---|
| 189 | }
|
---|
| 190 | }
|
---|
| 191 |
|
---|
| 192 | /**
|
---|
| 193 | * This method forces a refresh of the uddiConnectionInfo.xml file by retrieving the
|
---|
| 194 | * information from the UDDI NHIN server.
|
---|
| 195 | *
|
---|
| 196 | * @param part1 No real data - just a way to keep the document unique.
|
---|
| 197 | * @return True if the file was loaded false if it was not.
|
---|
| 198 | */
|
---|
| 199 | public static UDDIUpdateManagerForceRefreshResponseType forceRefreshFileFromUDDIServer(UDDIUpdateManagerForceRefreshRequestType part1)
|
---|
| 200 | {
|
---|
| 201 | UDDIUpdateManagerForceRefreshResponseType oResponse = new UDDIUpdateManagerForceRefreshResponseType();
|
---|
| 202 | oResponse.setSuccessOrFail(new SuccessOrFailType());
|
---|
| 203 | oResponse.getSuccessOrFail().setSuccess(false);
|
---|
| 204 |
|
---|
| 205 | try
|
---|
| 206 | {
|
---|
| 207 | forceRefreshUDDIFile();
|
---|
| 208 | oResponse.getSuccessOrFail().setSuccess(true); // If we got here - we succeeded.
|
---|
| 209 | }
|
---|
| 210 | catch (Exception e)
|
---|
| 211 | {
|
---|
| 212 | String sErrorMessage = "Failed to refresh the file from the UDDI server. Error: " + e.getMessage();
|
---|
| 213 | log.error(sErrorMessage, e);
|
---|
| 214 | }
|
---|
| 215 |
|
---|
| 216 | return oResponse;
|
---|
| 217 | }
|
---|
| 218 |
|
---|
| 219 | /**
|
---|
| 220 | * Main method to test this class. Since this is relys on an external UDDI server,
|
---|
| 221 | * we do not want it part of our unit tests to stop the build if the server is
|
---|
| 222 | * down or not accessible. This is a main method used for debugging....
|
---|
| 223 | * @param args
|
---|
| 224 | */
|
---|
| 225 | public static void main (String args[])
|
---|
| 226 | {
|
---|
| 227 | System.out.println("Starting test.");
|
---|
| 228 |
|
---|
| 229 | try
|
---|
| 230 | {
|
---|
| 231 | UDDIUpdateManagerForceRefreshRequestType part1 = new UDDIUpdateManagerForceRefreshRequestType();
|
---|
| 232 | UDDIUpdateManagerForceRefreshResponseType oResponse = forceRefreshFileFromUDDIServer(part1);
|
---|
| 233 | System.out.println("Response = " + oResponse.getSuccessOrFail().isSuccess());
|
---|
| 234 | }
|
---|
| 235 | catch (Exception e)
|
---|
| 236 | {
|
---|
| 237 | System.out.println("An unexpected error occurred: " + e.getMessage());
|
---|
| 238 | e.printStackTrace();
|
---|
| 239 | System.exit(-1);
|
---|
| 240 | }
|
---|
| 241 |
|
---|
| 242 | System.out.println("Ending test.");
|
---|
| 243 |
|
---|
| 244 | }
|
---|
| 245 | }
|
---|