[77] | 1 | package com.framsticks.core; |
---|
| 2 | |
---|
| 3 | import com.framsticks.communication.*; |
---|
| 4 | import com.framsticks.params.*; |
---|
| 5 | import com.framsticks.params.types.ObjectParam; |
---|
| 6 | import com.framsticks.parsers.Loaders; |
---|
| 7 | import com.framsticks.parsers.MultiParamLoader; |
---|
| 8 | import com.framsticks.util.*; |
---|
| 9 | import com.framsticks.util.UnsupportedOperationException; |
---|
[84] | 10 | import com.framsticks.util.dispatching.Dispatching; |
---|
| 11 | import com.framsticks.util.dispatching.Future; |
---|
| 12 | import com.framsticks.util.lang.Casting; |
---|
[77] | 13 | import org.apache.log4j.Logger; |
---|
| 14 | |
---|
| 15 | import java.util.*; |
---|
| 16 | |
---|
| 17 | /** |
---|
| 18 | * @author Piotr Sniegowski |
---|
| 19 | */ |
---|
| 20 | public abstract class Instance extends Entity { |
---|
| 21 | |
---|
[84] | 22 | private static final Logger log = Logger.getLogger(Instance.class.getName()); |
---|
[77] | 23 | |
---|
| 24 | |
---|
[84] | 25 | protected Node root; |
---|
[77] | 26 | |
---|
[84] | 27 | public Set<InstanceListener> listeners = new HashSet<InstanceListener>(); |
---|
[77] | 28 | |
---|
[84] | 29 | public Instance() { |
---|
[77] | 30 | } |
---|
| 31 | |
---|
[84] | 32 | @Override |
---|
| 33 | protected void run() { |
---|
| 34 | super.run(); |
---|
| 35 | root = new Node((CompositeParam)new ParamBuilder().setName("Instance").setId(name).setType("o").build(), null); |
---|
[78] | 36 | com.framsticks.model.Package.register(registry); |
---|
[84] | 37 | } |
---|
[77] | 38 | |
---|
[84] | 39 | protected void fetchInfo(Path path, Future<FramsClass> future) { |
---|
[78] | 40 | future.result(null, new UnsupportedOperationException()); |
---|
| 41 | } |
---|
[77] | 42 | |
---|
[78] | 43 | public void resolve(Path path, Future<Path> future) { |
---|
| 44 | assert isActive(); |
---|
| 45 | assert path.isOwner(this); |
---|
| 46 | if (path.getTop().getObject() != null) { |
---|
| 47 | future.result(path, null); |
---|
| 48 | return; |
---|
| 49 | } |
---|
| 50 | AccessInterface access = bindAccess(path.getUnder()); |
---|
| 51 | Object object = access.get(path.getTop().getParam(), Object.class); |
---|
| 52 | if (object == null) { |
---|
| 53 | future.result(path, null); |
---|
| 54 | return; |
---|
| 55 | } |
---|
| 56 | future.result(path.appendResolution(object), null); |
---|
| 57 | } |
---|
| 58 | |
---|
[84] | 59 | public void fetchValue(Path path, Param param, StateFunctor stateFunctor) { |
---|
[78] | 60 | stateFunctor.call(null); |
---|
| 61 | } |
---|
| 62 | |
---|
[84] | 63 | public void fetchValues(Path path, StateFunctor stateFunctor) { |
---|
[78] | 64 | stateFunctor.call(null); |
---|
| 65 | } |
---|
| 66 | |
---|
[84] | 67 | protected void tryRegisterOnChangeEvents(Path path) { |
---|
[78] | 68 | |
---|
| 69 | } |
---|
| 70 | |
---|
[77] | 71 | public void storeValue(Path path, Param param, Object value, final StateFunctor stateFunctor) { |
---|
| 72 | assert isActive(); |
---|
| 73 | invokeLater(new Runnable() { |
---|
| 74 | @Override |
---|
| 75 | public void run() { |
---|
| 76 | stateFunctor.call(new UnsupportedOperationException()); |
---|
| 77 | } |
---|
| 78 | }); |
---|
| 79 | } |
---|
| 80 | |
---|
[84] | 81 | protected void fireRun(Exception e) { |
---|
| 82 | for (InstanceListener l : this.listeners) { |
---|
| 83 | l.onRun(e); |
---|
| 84 | } |
---|
| 85 | } |
---|
[77] | 86 | |
---|
[84] | 87 | protected void fireStop(Exception e) { |
---|
| 88 | for (InstanceListener l : this.listeners) { |
---|
| 89 | l.onStop(e); |
---|
| 90 | } |
---|
| 91 | } |
---|
[77] | 92 | |
---|
[84] | 93 | public void addListener(final InstanceListener listener) { |
---|
| 94 | assert Dispatching.isThreadSafe(); |
---|
| 95 | Dispatching.invokeLaterOrNow(this, new Runnable() { |
---|
| 96 | @Override |
---|
| 97 | public void run() { |
---|
| 98 | listeners.add(listener); |
---|
| 99 | } |
---|
| 100 | }); |
---|
| 101 | } |
---|
[77] | 102 | |
---|
[84] | 103 | public void removeListener(final InstanceListener listener) { |
---|
| 104 | assert Dispatching.isThreadSafe(); |
---|
| 105 | Dispatching.invokeLaterOrNow(this, new Runnable() { |
---|
| 106 | @Override |
---|
| 107 | public void run() { |
---|
| 108 | listeners.remove(listener); |
---|
| 109 | } |
---|
| 110 | }); |
---|
| 111 | } |
---|
[77] | 112 | |
---|
| 113 | |
---|
[84] | 114 | protected void fireListChange(Path path, ListChange change) { |
---|
| 115 | assert isActive(); |
---|
| 116 | for (InstanceListener l : this.listeners) { |
---|
[77] | 117 | l.onListChange(path, change); |
---|
| 118 | } |
---|
| 119 | } |
---|
| 120 | |
---|
[84] | 121 | protected void fireFetch(Path path) { |
---|
| 122 | assert isActive(); |
---|
| 123 | for (InstanceListener l : this.listeners) { |
---|
| 124 | l.onFetch(path); |
---|
| 125 | } |
---|
| 126 | } |
---|
[77] | 127 | |
---|
| 128 | |
---|
[84] | 129 | public final FramsClass getInfoFromCache(Path path) { |
---|
| 130 | return getInfoFromCache(path.getTop().getParam().getContainedTypeName()); |
---|
| 131 | } |
---|
[77] | 132 | |
---|
[84] | 133 | |
---|
| 134 | public FramsClass getInfoFromCache(String id) { |
---|
| 135 | assert isActive(); |
---|
[78] | 136 | return registry.getInfoFromCache(id); |
---|
[77] | 137 | } |
---|
| 138 | |
---|
[78] | 139 | protected Registry registry = new Registry(); |
---|
[77] | 140 | |
---|
| 141 | public AccessInterface createAccess(String name) { |
---|
[84] | 142 | assert isActive(); |
---|
| 143 | return registry.createAccess(name); |
---|
| 144 | } |
---|
[77] | 145 | |
---|
[84] | 146 | |
---|
| 147 | // TODO: make ValueParam |
---|
| 148 | public <T> T get(Node node, Param childParam, Class<T> type) { |
---|
| 149 | return bindAccess(node).get((ValueParam) childParam, type); |
---|
[77] | 150 | } |
---|
| 151 | |
---|
[84] | 152 | public void findInfo(final Path path, final Future<FramsClass> future) { |
---|
| 153 | assert isActive(); |
---|
| 154 | final String name = path.getTop().getParam().getContainedTypeName(); |
---|
| 155 | final FramsClass framsClass = getInfoFromCache(name); |
---|
| 156 | if (framsClass != null) { |
---|
| 157 | log.trace("info for " + name + " found in cache"); |
---|
| 158 | future.result(framsClass, null); |
---|
| 159 | return; |
---|
| 160 | } |
---|
| 161 | fetchInfo(path, future); |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | public final AccessInterface bindAccess(Node node) { |
---|
| 165 | assert node.getObject() != null; |
---|
| 166 | AccessInterface access = registry.prepareAccess(node.getParam()); |
---|
| 167 | if (access == null) { |
---|
| 168 | log.error("missing access for: " + node.getParam()); |
---|
[77] | 169 | return null; |
---|
| 170 | } |
---|
[84] | 171 | access.select(node.getObject()); |
---|
| 172 | return access; |
---|
[77] | 173 | } |
---|
| 174 | |
---|
[84] | 175 | public final <T> T getParam(Path path, String id, Class<T> type) { |
---|
| 176 | return Casting.tryCast(type, registry.prepareAccess(path.getTop().getParam()).getParam(id)); |
---|
| 177 | } |
---|
[77] | 178 | |
---|
[84] | 179 | public final AccessInterface bindAccess(Path path) { |
---|
| 180 | assert path.isResolved(); |
---|
| 181 | return bindAccess(path.getTop()); |
---|
| 182 | } |
---|
[77] | 183 | |
---|
[84] | 184 | public void resolve(final String targetPath, final Future<Path> future) { |
---|
| 185 | assert isActive(); |
---|
| 186 | final Path path = getPath(targetPath); |
---|
| 187 | resolve(path, new Future<Path>() { |
---|
| 188 | @Override |
---|
| 189 | public void result(Path result, Exception e) { |
---|
| 190 | assert isActive(); |
---|
| 191 | if (e != null) { |
---|
| 192 | future.result(path, e); |
---|
| 193 | return; |
---|
| 194 | } |
---|
| 195 | if (path.isResolved(targetPath)) { |
---|
| 196 | future.result(path, null); |
---|
| 197 | return; |
---|
| 198 | } |
---|
| 199 | if (path.isResolved()) { |
---|
| 200 | future.result(path, new Exception("testing")); |
---|
| 201 | return; |
---|
| 202 | } |
---|
| 203 | resolve(targetPath, future); |
---|
| 204 | } |
---|
| 205 | }); |
---|
| 206 | } |
---|
[77] | 207 | |
---|
[84] | 208 | public void resolveAndFetch(final String targetPath, final Future<Path> future) { |
---|
| 209 | assert isActive(); |
---|
| 210 | resolve(targetPath, new Future<Path>() { |
---|
| 211 | @Override |
---|
| 212 | public void result(final Path path, Exception e) { |
---|
| 213 | if (e != null) { |
---|
| 214 | future.result(path, e); |
---|
| 215 | return; |
---|
| 216 | } |
---|
| 217 | assert path.isResolved(targetPath); |
---|
| 218 | fetchValues(path, new StateFunctor() { |
---|
| 219 | @Override |
---|
| 220 | public void call(Exception e) { |
---|
| 221 | future.result(path, e); |
---|
| 222 | } |
---|
| 223 | }); |
---|
| 224 | } |
---|
| 225 | }); |
---|
| 226 | } |
---|
[77] | 227 | |
---|
[84] | 228 | public Path createIfNeeded(String path) { |
---|
| 229 | Path p; |
---|
| 230 | while (!(p = getPath(path)).isResolved(path)) { |
---|
| 231 | create(p); |
---|
| 232 | } |
---|
| 233 | return p; |
---|
| 234 | } |
---|
[77] | 235 | |
---|
[84] | 236 | public Path createIfNeeded(Path path) { |
---|
| 237 | assert isActive(); |
---|
| 238 | if (path.isResolved()) { |
---|
| 239 | return path; |
---|
| 240 | } |
---|
| 241 | return create(path); |
---|
| 242 | } |
---|
[77] | 243 | |
---|
[84] | 244 | public Path create(Path path) { |
---|
| 245 | assert isActive(); |
---|
| 246 | assert !path.isResolved(); |
---|
| 247 | Path resolved = path.tryFindResolution(); |
---|
| 248 | if (!resolved.isResolved()) { |
---|
| 249 | log.debug("creating: " + path); |
---|
| 250 | AccessInterface access = registry.prepareAccess(path.getTop().getParam()); |
---|
| 251 | assert access != null; |
---|
| 252 | Object child = access.createAccessee(); |
---|
| 253 | assert child != null; |
---|
| 254 | if (path.size() == 1) { |
---|
| 255 | root = new Node(root.getParam(), child); |
---|
| 256 | } else { |
---|
| 257 | bindAccess(path.getUnder()).set(path.getTop().getParam(), child); |
---|
| 258 | } |
---|
| 259 | resolved = path.appendResolution(child); |
---|
| 260 | } |
---|
| 261 | tryRegisterOnChangeEvents(resolved); |
---|
| 262 | return resolved; |
---|
| 263 | } |
---|
[77] | 264 | |
---|
| 265 | |
---|
| 266 | |
---|
| 267 | |
---|
[84] | 268 | public FramsClass processFetchedInfo(File file) { |
---|
| 269 | assert isActive(); |
---|
| 270 | FramsClass framsClass = Loaders.loadFramsClass(file.getContent()); |
---|
| 271 | if ("/".equals(file.getPath())) { |
---|
| 272 | if (root.getParam().getContainedTypeName() == null) { |
---|
| 273 | root = new Node((CompositeParam)new ParamBuilder().setName("Instance").setId(name).setType("o " + framsClass.getId()).build(), root.getObject()); |
---|
| 274 | } |
---|
| 275 | } |
---|
| 276 | registry.putInfoIntoCache(framsClass); |
---|
| 277 | return framsClass; |
---|
| 278 | } |
---|
[77] | 279 | |
---|
[84] | 280 | public void processFetchedValues(Path path, List<File> files) { |
---|
| 281 | assert isActive(); |
---|
| 282 | assert files.size() == 1; |
---|
| 283 | assert path.isTheSame(files.get(0).getPath()); |
---|
| 284 | Node node = path.getTop(); |
---|
| 285 | MultiParamLoader loader = new MultiParamLoader(); |
---|
| 286 | loader.setNewSource(files.get(0).getContent()); |
---|
| 287 | loader.addBreakCondition(MultiParamLoader.Status.AfterObject); |
---|
[77] | 288 | |
---|
[84] | 289 | try { |
---|
| 290 | if (node.getParam() instanceof ObjectParam) { |
---|
| 291 | loader.addAccessInterface(bindAccess(node)); |
---|
| 292 | loader.go(); |
---|
| 293 | fireFetch(path); |
---|
| 294 | // for (NodeListener l : listeners) { |
---|
| 295 | // l.onChange(this); |
---|
| 296 | // } |
---|
| 297 | return; |
---|
| 298 | } |
---|
[77] | 299 | |
---|
[84] | 300 | ListAccess listAccess = ((ListAccess)bindAccess(node)); |
---|
| 301 | assert listAccess != null; |
---|
| 302 | listAccess.clearValues(); |
---|
[77] | 303 | |
---|
[84] | 304 | AccessInterface elementAccess = listAccess.getElementAccess(); |
---|
| 305 | loader.addAccessInterface(elementAccess); |
---|
| 306 | MultiParamLoader.Status status; |
---|
| 307 | while ((status = loader.go()) != MultiParamLoader.Status.Finished) { |
---|
| 308 | if (status == MultiParamLoader.Status.AfterObject) { |
---|
| 309 | AccessInterface accessInterface = loader.getLastAccessInterface(); |
---|
[77] | 310 | |
---|
[84] | 311 | String id = listAccess.computeIdentifierFor(accessInterface.getSelected()); |
---|
| 312 | Param param = new ParamBuilder().setType("o " + accessInterface.getId()).setId(id).build(); |
---|
| 313 | Object child = accessInterface.getSelected(); |
---|
| 314 | accessInterface.select(null); |
---|
| 315 | assert child != null; |
---|
| 316 | bindAccess(node).set((ValueParam) param, child); |
---|
| 317 | } |
---|
| 318 | } |
---|
[77] | 319 | |
---|
[84] | 320 | fireFetch(path); |
---|
| 321 | // for (NodeListener l : listeners) { |
---|
| 322 | // l.onChange(this); |
---|
| 323 | // } |
---|
| 324 | } catch (Exception e) { |
---|
| 325 | log.error("exception occurred while loading: " + e); |
---|
| 326 | } |
---|
[77] | 327 | |
---|
[84] | 328 | } |
---|
[77] | 329 | |
---|
[84] | 330 | public static Iterator<String> splitPath(String path) { |
---|
| 331 | List<String> list = new LinkedList<String>(); |
---|
| 332 | for (String s : path.split("/")) { |
---|
| 333 | if (!s.isEmpty()) { |
---|
| 334 | list.add(s); |
---|
| 335 | } |
---|
| 336 | } |
---|
| 337 | return list.iterator(); |
---|
| 338 | } |
---|
[77] | 339 | |
---|
[78] | 340 | public Registry getRegistry() { |
---|
| 341 | return registry; |
---|
| 342 | } |
---|
[84] | 343 | |
---|
| 344 | public Path getPath(String textual) { |
---|
| 345 | return new Path(this, textual); |
---|
| 346 | } |
---|
| 347 | |
---|
| 348 | public Path getRootPath() { |
---|
| 349 | return getPath("/"); |
---|
| 350 | } |
---|
[77] | 351 | } |
---|
[78] | 352 | |
---|