[3661] | 1 | package org.greenstone.gsdl3.build;
|
---|
| 2 |
|
---|
[24883] | 3 | import java.io.File;
|
---|
| 4 |
|
---|
[3661] | 5 | import org.greenstone.gsdl3.util.*;
|
---|
| 6 | //import java.util.Thread
|
---|
[31669] | 7 | import java.util.concurrent.CopyOnWriteArrayList;
|
---|
[3661] | 8 | import org.w3c.dom.Element;
|
---|
| 9 |
|
---|
[3683] | 10 | /** base class for collection construction */
|
---|
[25537] | 11 | public abstract class CollectionConstructor extends Thread
|
---|
| 12 | {
|
---|
[3661] | 13 |
|
---|
[25537] | 14 | /** the site in which building is to take place */
|
---|
| 15 | protected String site_home = null;
|
---|
| 16 | /** the name of the site */
|
---|
| 17 | protected String site_name = null;
|
---|
| 18 | /** the name of the collection */
|
---|
| 19 | protected String collection_name = null;
|
---|
| 20 | /** the stage of construction */
|
---|
| 21 | protected int process_type = -1;
|
---|
| 22 | /** other arguments/parameters for the construction process - in a paramList */
|
---|
| 23 | protected Element process_params = null;
|
---|
[31669] | 24 | /** the list of listeners for the process. We need it to be threadsafe.
|
---|
| 25 | * see http://stackoverflow.com/questions/8259479/should-i-synchronize-listener-notifications-or-not
|
---|
| 26 | * https://docs.oracle.com/javase/6/docs/api/java/util/concurrent/CopyOnWriteArrayList.html
|
---|
| 27 | * "A thread-safe variant of ArrayList in which all mutative operations (add, set, and so on) are
|
---|
| 28 | * implemented by making a fresh copy of the underlying array.
|
---|
| 29 | * This is ordinarily too costly, but may be more efficient than alternatives when traversal operations
|
---|
| 30 | * vastly outnumber mutations, and is useful when you cannot or don't want to synchronize traversals,
|
---|
| 31 | * yet need to preclude interference among concurrent threads."
|
---|
| 32 | */
|
---|
| 33 | protected final CopyOnWriteArrayList<ConstructionListener> listeners;
|
---|
[25537] | 34 | /** A flag used to determine if this process has been asked to cancel. */
|
---|
[31578] | 35 | protected boolean cancel = false; // Not really used (in any way that works)
|
---|
[25538] | 36 | /** Stores the name of the manifest file (if one is needed) */
|
---|
| 37 | protected String manifest_file = null;
|
---|
[31578] | 38 | /** The URL params constructed as a query string, representing the CGI QUERY_STRING to the process */
|
---|
| 39 | protected String query_string = null;
|
---|
[3661] | 40 |
|
---|
[25537] | 41 | public CollectionConstructor(String name)
|
---|
| 42 | {
|
---|
| 43 | super(name);
|
---|
[31669] | 44 | this.listeners = new CopyOnWriteArrayList<ConstructionListener>();
|
---|
[3661] | 45 | }
|
---|
[25537] | 46 |
|
---|
| 47 | /**
|
---|
| 48 | * carry out any set up stuff - returns false if couldn't set up properly
|
---|
| 49 | */
|
---|
| 50 | public boolean configure()
|
---|
| 51 | {
|
---|
| 52 | return true;
|
---|
[3661] | 53 | }
|
---|
| 54 |
|
---|
[31572] | 55 | // this method never gets called. And, the way subclass GS2PerlConstructor.runPerlCommand() was originally
|
---|
| 56 | // coded, setting cancel to true never had any effect anyway in stopping any perl command that was run.
|
---|
[25537] | 57 | public void stopAction()
|
---|
| 58 | {
|
---|
| 59 | this.cancel = true;
|
---|
[3661] | 60 | }
|
---|
| 61 |
|
---|
[25537] | 62 | public void setActionType(int type)
|
---|
| 63 | {
|
---|
| 64 | this.process_type = type;
|
---|
[3661] | 65 | }
|
---|
[25537] | 66 |
|
---|
| 67 | public void setSiteHome(String site_home)
|
---|
| 68 | {
|
---|
| 69 | this.site_home = site_home;
|
---|
| 70 |
|
---|
| 71 | File siteHomeFile = new File(site_home);
|
---|
| 72 | this.site_name = siteHomeFile.getName();
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | public void setCollectionName(String coll_name)
|
---|
| 76 | {
|
---|
| 77 | this.collection_name = coll_name;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
[29869] | 80 | public void setQueryString(String querystring)
|
---|
| 81 | {
|
---|
| 82 | this.query_string = querystring;
|
---|
| 83 | }
|
---|
| 84 |
|
---|
[25537] | 85 | public void setProcessParams(Element params)
|
---|
| 86 | {
|
---|
| 87 | this.process_params = params;
|
---|
| 88 | }
|
---|
[25538] | 89 |
|
---|
| 90 | public void setManifestFile(String manifestFile)
|
---|
| 91 | {
|
---|
| 92 | this.manifest_file = manifestFile;
|
---|
| 93 | }
|
---|
[25537] | 94 |
|
---|
| 95 | public boolean addListener(ConstructionListener listener)
|
---|
| 96 | {
|
---|
[31669] | 97 | this.listeners.add(listener);
|
---|
| 98 | return true;
|
---|
[25537] | 99 | }
|
---|
| 100 |
|
---|
[31671] | 101 | // We never call removeListener. If we do start calling removeListener() change the listeners list
|
---|
| 102 | // over to type ConcurrentLinkedQueue, for reasons explained at
|
---|
| 103 | // http://stackoverflow.com/questions/8259479/should-i-synchronize-listener-notifications-or-not
|
---|
| 104 | // The current listeners list type is CopyOnWriteArrayList, which provides thread safety. But it
|
---|
| 105 | // can still send off events to listeners just as they're being unregistered, and that could be a
|
---|
| 106 | // problem if we were specifically removing the listener because we wanted to cease it from
|
---|
| 107 | // listening and responding to subsequent events.
|
---|
[25537] | 108 | public boolean removeListener(ConstructionListener listener)
|
---|
[31669] | 109 | {
|
---|
| 110 | this.listeners.remove(listener);
|
---|
| 111 | return true;
|
---|
[25537] | 112 | }
|
---|
| 113 |
|
---|
| 114 | protected void sendProcessBegun(ConstructionEvent evt)
|
---|
| 115 | {
|
---|
[31669] | 116 | // See http://stackoverflow.com/questions/8259479/should-i-synchronize-listener-notifications-or-not
|
---|
| 117 | for (ConstructionListener l: this.listeners) {
|
---|
| 118 | l.processBegun(evt);
|
---|
| 119 | }
|
---|
[25537] | 120 | }
|
---|
| 121 |
|
---|
| 122 | protected void sendProcessComplete(ConstructionEvent evt)
|
---|
| 123 | {
|
---|
[31669] | 124 | for (ConstructionListener l: this.listeners) {
|
---|
| 125 | l.processComplete(evt);
|
---|
| 126 | }
|
---|
[25537] | 127 | }
|
---|
| 128 |
|
---|
[31669] | 129 | // Method doesn't need to be synchronized any more, since it uses the ThreadSafe CopyOnWriteArrayList
|
---|
| 130 | // for listeners list.
|
---|
| 131 | // See http://stackoverflow.com/questions/8259479/should-i-synchronize-listener-notifications-or-not
|
---|
| 132 | // See http://stackoverflow.com/questions/574240/is-there-an-advantage-to-use-a-synchronized-method-instead-of-a-synchronized-blo
|
---|
| 133 | protected void sendProcessStatus(ConstructionEvent evt)
|
---|
[25537] | 134 | {
|
---|
[31669] | 135 | for (ConstructionListener l: this.listeners) {
|
---|
| 136 | l.processStatus(evt);
|
---|
| 137 | }
|
---|
[25537] | 138 | }
|
---|
| 139 |
|
---|
| 140 | protected void sendMessage(ConstructionEvent evt)
|
---|
| 141 | {
|
---|
[31669] | 142 | for (ConstructionListener l: this.listeners) {
|
---|
| 143 | l.message(evt);
|
---|
| 144 | }
|
---|
[25537] | 145 | }
|
---|
| 146 |
|
---|
| 147 | abstract public void run();
|
---|
[3661] | 148 | }
|
---|