1 | package gov.hhs.fha.nhinc.connectmgr.uddi;
|
---|
2 |
|
---|
3 | import java.util.Timer;
|
---|
4 |
|
---|
5 | import gov.hhs.fha.nhinc.properties.PropertyAccessor;
|
---|
6 |
|
---|
7 | import java.util.logging.Level;
|
---|
8 | import java.util.logging.Logger;
|
---|
9 | import org.apache.commons.logging.Log;
|
---|
10 | import org.apache.commons.logging.LogFactory;
|
---|
11 |
|
---|
12 | /**
|
---|
13 | * This class is used to start a timer which when it wakes up will read the UDDI
|
---|
14 | * data from the UDDI server, updte the uddiConnectionInfo.xml file with that data,
|
---|
15 | * and then tell the ConnectionManager to refresh the UDDI cache with it.
|
---|
16 | *
|
---|
17 | * @author Les Westberg
|
---|
18 | */
|
---|
19 | public class UDDITimer extends Thread
|
---|
20 | {
|
---|
21 | private static Log log = LogFactory.getLog(UDDITimer.class);
|
---|
22 | private static UDDITimer m_oTheOneAndOnlyTimer = null;
|
---|
23 | private static boolean m_bRunnable = false;
|
---|
24 | private static final String GATEWAY_PROPERTY_FILE = "gateway";
|
---|
25 | private static final String UDDI_REFRESH_DURATION_PROPERTY = "UDDIRefreshDuration";
|
---|
26 | private static final int UDDI_REFRESH_DURATION_DEFAULT = 1800; //(30 minutes)
|
---|
27 |
|
---|
28 | // private Timer m_oTimer = new Timer(true); // Timer thread - set up as a daemon.
|
---|
29 | private int m_iDurationSeconds = UDDI_REFRESH_DURATION_DEFAULT;
|
---|
30 |
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Default constructor
|
---|
34 | */
|
---|
35 | private UDDITimer()
|
---|
36 | {
|
---|
37 | }
|
---|
38 |
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * This method is used to crete an instance of the UDDITimer. There should only be exactly
|
---|
42 | * one instance of this running at any time. If it exists, it simply returns the one that
|
---|
43 | * exists. If it does not exist, then it will create it, start the timer, and return a handle to
|
---|
44 | * it.
|
---|
45 | *
|
---|
46 | * @throws UDDIAccessorException
|
---|
47 | */
|
---|
48 | public static void startTimer()
|
---|
49 | throws UDDIAccessorException
|
---|
50 | {
|
---|
51 | m_bRunnable = true;
|
---|
52 |
|
---|
53 | if (m_oTheOneAndOnlyTimer == null)
|
---|
54 | {
|
---|
55 | m_oTheOneAndOnlyTimer = new UDDITimer();
|
---|
56 | try
|
---|
57 | {
|
---|
58 | m_oTheOneAndOnlyTimer.initialize();
|
---|
59 | m_oTheOneAndOnlyTimer.setDaemon(true);
|
---|
60 | m_oTheOneAndOnlyTimer.start();
|
---|
61 | }
|
---|
62 | catch (Exception e)
|
---|
63 | {
|
---|
64 | m_oTheOneAndOnlyTimer = null;
|
---|
65 | String sErrorMessage = "Failed to start the UDDI Update Manager timer. Error: " + e.getMessage();
|
---|
66 | log.error(sErrorMessage, e);
|
---|
67 | throw new UDDIAccessorException(sErrorMessage, e);
|
---|
68 | }
|
---|
69 |
|
---|
70 | log.info("UDDIUpdateManager timer has just been started now.");
|
---|
71 | }
|
---|
72 | }
|
---|
73 |
|
---|
74 | public static void stopTimer()
|
---|
75 | {
|
---|
76 | m_bRunnable = false;
|
---|
77 | }
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * This method starts up the timer.
|
---|
81 | */
|
---|
82 | private void initialize()
|
---|
83 | throws UDDIAccessorException
|
---|
84 | {
|
---|
85 |
|
---|
86 | try
|
---|
87 | {
|
---|
88 | String sDuration = PropertyAccessor.getProperty(GATEWAY_PROPERTY_FILE, UDDI_REFRESH_DURATION_PROPERTY);
|
---|
89 | if ((sDuration != null) && (sDuration.length() > 0))
|
---|
90 | {
|
---|
91 | m_iDurationSeconds = Integer.parseInt(sDuration);
|
---|
92 | }
|
---|
93 | }
|
---|
94 | catch (Exception e)
|
---|
95 | {
|
---|
96 | String sErrorMessage = "Failed to read and parse " + UDDI_REFRESH_DURATION_PROPERTY +
|
---|
97 | " from " + GATEWAY_PROPERTY_FILE + ".properties file - using default " + "" +
|
---|
98 | "value of " + UDDI_REFRESH_DURATION_DEFAULT + " seconds. Error: " +
|
---|
99 | e.getMessage();
|
---|
100 | log.warn(sErrorMessage, e);
|
---|
101 | }
|
---|
102 |
|
---|
103 | // UDDITimerTask oUDDITimerTask = new UDDITimerTask();
|
---|
104 | // m_oTimer.schedule(oUDDITimerTask, 0, iDurationSeconds*1000);
|
---|
105 |
|
---|
106 | }
|
---|
107 |
|
---|
108 | @Override
|
---|
109 | public void run()
|
---|
110 | {
|
---|
111 | while (m_bRunnable)
|
---|
112 | {
|
---|
113 | UDDITimerTask oUDDITimerTask = new UDDITimerTask();
|
---|
114 | oUDDITimerTask.run();
|
---|
115 | try
|
---|
116 | {
|
---|
117 | log.debug("Before reading properties wait status....");
|
---|
118 | Thread.sleep(m_iDurationSeconds * 1000);
|
---|
119 | log.debug("Now read properties....");
|
---|
120 | }
|
---|
121 | catch (InterruptedException ex)
|
---|
122 | {
|
---|
123 | log.error("Failed to sleep.", ex);
|
---|
124 | }
|
---|
125 | }
|
---|
126 | }
|
---|
127 |
|
---|
128 | /**
|
---|
129 | * Main method used to test this class. This one really should not be run under unit
|
---|
130 | * test scenarios because it requires access to the UDDI server.
|
---|
131 | *
|
---|
132 | * @param args
|
---|
133 | */
|
---|
134 | public static void main (String[] args)
|
---|
135 | {
|
---|
136 | System.out.println("Starting test.");
|
---|
137 | log.debug("Log: Starting test.");
|
---|
138 |
|
---|
139 | try
|
---|
140 | {
|
---|
141 | UDDITimer.startTimer();
|
---|
142 | Thread.sleep(70000); // 1 minutes 10 seconds
|
---|
143 | }
|
---|
144 | catch (Exception e)
|
---|
145 | {
|
---|
146 | System.out.println("An unexpected exception occurred: " + e.getMessage());
|
---|
147 | e.printStackTrace();
|
---|
148 | System.exit(-1);
|
---|
149 | }
|
---|
150 |
|
---|
151 | System.out.println("End of test.");
|
---|
152 |
|
---|
153 | }
|
---|
154 | }
|
---|