1
2
3
4
5
6
7
8 package com.buckosoft.PicMan.business.mosaic;
9
10 import java.util.Iterator;
11 import java.util.LinkedList;
12 import java.util.List;
13
14 import org.apache.commons.logging.Log;
15 import org.apache.commons.logging.LogFactory;
16
17 import com.buckosoft.PicMan.business.PicManFacade;
18 import com.buckosoft.PicMan.domain.Mosaic;
19
20
21
22
23
24
25
26 public class MosaicMan {
27 private static boolean DEBUG = false;
28 private static boolean DEBUGEngine = false;
29 protected final Log logger = LogFactory.getLog(getClass());
30
31 private PicManFacade pmf;
32
33 private boolean building = false;
34
35 private Mosaic mosaicBuilding;
36 private MosaicEngine mosaicEngine;
37
38 private MosaicManDevelopment mosaicManDevelopment = new MosaicManDevelopment();
39
40
41 private LinkedList<Mosaic> mosaicList = new LinkedList<Mosaic>();
42
43
44
45
46 public void setPicMan(PicManFacade pmf) {
47 this.pmf = pmf;
48 mosaicManDevelopment.setPicMan(pmf);
49 }
50
51
52
53
54 public void setDEBUG(boolean debugFlag) {
55 DEBUG = debugFlag;
56 }
57
58
59
60
61 public void setDEBUGEngine(boolean debugFlag) {
62 DEBUGEngine = debugFlag;
63 }
64
65
66
67
68 public MosaicManDevelopment getMosaicManDevelopment() {
69 return mosaicManDevelopment;
70 }
71
72
73
74
75 public MosaicEngine getMosaicEngine() {
76 return mosaicEngine;
77 }
78
79
80
81
82
83
84 private synchronized void addToMosaicList(Mosaic mosaic) {
85 if (mosaic == null) {
86 if (mosaicList.size() > 0)
87 mosaicList.removeFirst();
88 return;
89 }
90 mosaicList.add(mosaic);
91 }
92
93
94
95
96 public boolean isBuilding() {
97 return(this.building);
98 }
99
100
101
102
103 public boolean hasWorkToDo() {
104 return(this.mosaicList.size() != 0);
105 }
106
107
108
109
110 public List<String> getMosaicQueueListString() {
111 LinkedList<String> list = new LinkedList<String>();
112 Iterator<Mosaic> iter = mosaicList.iterator();
113 while (iter.hasNext()) {
114 Mosaic m = iter.next();
115 list.add(m.getName());
116 }
117 return(list);
118 }
119
120
121
122
123 public void queueForBuilding(Mosaic mosaic) {
124 this.addToMosaicList(mosaic);
125 if (DEBUG)
126 logger.info("Start building " + mosaic.getName());
127 }
128
129
130
131
132 public int emptyMosaicQueue() {
133 int i = this.mosaicList.size();
134 this.mosaicList.clear();
135 return(i);
136 }
137
138
139
140
141
142
143
144 public boolean runBuilder() throws Exception {
145 boolean moreWork = false;
146 this.mosaicBuilding = this.mosaicList.getFirst();
147 if (this.mosaicBuilding == null)
148 return(false);
149 try {
150 mosaicEngine = (MosaicEngine)Class.forName("com.buckosoft.PicMan.business.mosaic.engine."
151 + mosaicBuilding.getEngine()).newInstance();
152 } catch (Exception e) {
153 Exception ex = new Exception("Can't instantiate Mosaic Engine: " + mosaicEngine, e);
154 pmf.addError(ex);
155 return(false);
156 }
157 mosaicEngine.setPicMan(pmf);
158 mosaicEngine.setDEBUG(DEBUGEngine);
159 mosaicEngine.setMosaic(this.mosaicBuilding);
160 pmf.setupMosaicThumbCache(this.mosaicBuilding.getSid(), this.mosaicBuilding.getTileHeight());
161 this.building = true;
162 try {
163 moreWork = mosaicEngine.build();
164 } catch (Exception e) {
165 this.building = false;
166 this.mosaicEngine = null;
167 this.addToMosaicList(null);
168 throw e;
169 }
170 this.building = moreWork;
171 if (!moreWork) {
172 this.mosaicEngine = null;
173 this.addToMosaicList(null);
174 this.pmf.releaseMosaicThumbCache();
175 }
176 return(moreWork);
177 }
178
179
180
181 public void outputMosaicFile() {
182 if (this.mosaicEngine == null) {
183 Exception e = new Exception("No mosaic to save");
184 pmf.addError(e);
185 return;
186 }
187
188 this.mosaicEngine.outputMosaicFile(false);
189 }
190 }