001    package de.deepamehta.core.service;
002    
003    import de.deepamehta.core.JSONEnabled;
004    import de.deepamehta.core.util.DeepaMehtaUtils;
005    
006    import org.codehaus.jettison.json.JSONObject;
007    
008    import java.util.ArrayList;
009    import java.util.Iterator;
010    import java.util.List;
011    import java.util.logging.Logger;
012    
013    
014    
015    public class Directives implements Iterable<Directives.Entry>, JSONEnabled {
016    
017        // ---------------------------------------------------------------------------------------------- Instance Variables
018    
019        private List<Entry> directives = new ArrayList();
020    
021        // ------------------------------------------------------------------------------------------------- Class Variables
022    
023        private static Logger logger = Logger.getLogger("de.deepamehta.core.service.Directives");
024    
025        private static final ThreadLocal<Directives> threadLocalDirectives = new ThreadLocal() {
026            @Override
027            protected Directives initialValue() {
028                logger.fine("### Creating tread-local directives");
029                return new Directives();
030            }
031        };
032    
033        // -------------------------------------------------------------------------------------------------- Public Methods
034    
035        public void add(Directive dir, JSONEnabled arg) {
036            directives.add(new Entry(dir, arg));
037        }
038    
039        // ---
040    
041        public static Directives get() {
042            return threadLocalDirectives.get();
043        }
044    
045        public static void remove() {
046            threadLocalDirectives.remove();
047        }
048    
049        // *** JSONEnabled Implementation ***
050    
051        public JSONObject toJSON() {
052            try {
053                JSONObject obj = new JSONObject();
054                obj.put("directives", DeepaMehtaUtils.objectsToJSON(directives));
055                return obj;
056            } catch (Exception e) {
057                throw new RuntimeException("Serialization failed (" + this + ")", e);
058            }
059        }
060    
061        // *** Iterable Implementation ***
062    
063        @Override
064        public Iterator<Entry> iterator() {
065            return directives.iterator();
066        }
067    
068        // --------------------------------------------------------------------------------------------------- Inner Classes
069    
070        public class Entry implements JSONEnabled {
071    
072            public Directive dir;
073            public JSONEnabled arg;
074    
075            private Entry(Directive dir, JSONEnabled arg) {
076                this.dir = dir;
077                this.arg = arg;
078            }
079    
080            @Override
081            public JSONObject toJSON() {
082                try {
083                    JSONObject obj = new JSONObject();
084                    obj.put("type", dir);
085                    obj.put("arg", arg.toJSON());
086                    return obj;
087                } catch (Exception e) {
088                    throw new RuntimeException("Serialization failed (" + this + ")", e);
089                }
090            }
091    
092            @Override
093            public String toString() {
094                return dir + ": " + arg;
095            }
096        }
097    }