annotate src/main/java/app/bbs/NetworkJungleBulletinBoard.java @ 123:495ac60d7f5f

Modified JungleUpdater
author one
date Fri, 27 Dec 2013 20:13:37 +0900
parents cef245f71053
children dacfa7eba841
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
112
8f9811a1e00c Moved app files
one
parents: 105
diff changeset
1 package app.bbs;
38
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
2
118
f64ff5bd66f5 Implements persistent for bbs app and Fixed bug JungleUpdater
one
parents: 112
diff changeset
3 import java.io.File;
f64ff5bd66f5 Implements persistent for bbs app and Fixed bug JungleUpdater
one
parents: 112
diff changeset
4 import java.io.FileNotFoundException;
f64ff5bd66f5 Implements persistent for bbs app and Fixed bug JungleUpdater
one
parents: 112
diff changeset
5 import java.io.IOException;
38
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
6 import java.nio.ByteBuffer;
56
ccfe9b5e8f11 bug LogUpdateCodeSegment
one
parents: 52
diff changeset
7 import java.util.Date;
38
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
8 import java.util.concurrent.atomic.AtomicInteger;
39
10359a815068 add alice.jungle.codesegment.remote
one
parents: 38
diff changeset
9
10359a815068 add alice.jungle.codesegment.remote
one
parents: 38
diff changeset
10 import alice.jungle.core.NetworkDefaultJungle;
118
f64ff5bd66f5 Implements persistent for bbs app and Fixed bug JungleUpdater
one
parents: 112
diff changeset
11 import alice.jungle.persistent.PersistentJournal;
f64ff5bd66f5 Implements persistent for bbs app and Fixed bug JungleUpdater
one
parents: 112
diff changeset
12 import alice.jungle.transaction.JungleUpdater;
38
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
13 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.Jungle;
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
14 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTree;
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
15 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTreeEditor;
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
16 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.core.Children;
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
17 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.core.Node;
118
f64ff5bd66f5 Implements persistent for bbs app and Fixed bug JungleUpdater
one
parents: 112
diff changeset
18 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.persistent.ChangeList;
f64ff5bd66f5 Implements persistent for bbs app and Fixed bug JungleUpdater
one
parents: 112
diff changeset
19 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.persistent.ChangeListReader;
38
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
20 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultNodePath;
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
21 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultTreeEditor;
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
22 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.EditableNode;
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
23 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.NodeEditor;
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
24 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser.DefaultTraverser;
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
25 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.DefaultEither;
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
26 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Either;
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
27 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error;
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
28 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.IterableConverter;
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
29
39
10359a815068 add alice.jungle.codesegment.remote
one
parents: 38
diff changeset
30 public class NetworkJungleBulletinBoard implements BulletinBoard
38
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
31 {
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
32 private final Jungle jungle;
118
f64ff5bd66f5 Implements persistent for bbs app and Fixed bug JungleUpdater
one
parents: 112
diff changeset
33 private final PersistentJournal journal;
122
cef245f71053 Implemented NetworkJungleBulletinBoard.checkAndCreateLogDirectory method
one
parents: 118
diff changeset
34 private final String LOG_DIR = "./log";
38
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
35
118
f64ff5bd66f5 Implements persistent for bbs app and Fixed bug JungleUpdater
one
parents: 112
diff changeset
36 public NetworkJungleBulletinBoard(String _uuid) throws FileNotFoundException
38
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
37 {
118
f64ff5bd66f5 Implements persistent for bbs app and Fixed bug JungleUpdater
one
parents: 112
diff changeset
38 journal = new PersistentJournal();
f64ff5bd66f5 Implements persistent for bbs app and Fixed bug JungleUpdater
one
parents: 112
diff changeset
39 jungle = new NetworkDefaultJungle(journal, _uuid,new DefaultTreeEditor(new DefaultTraverser()));
123
495ac60d7f5f Modified JungleUpdater
one
parents: 122
diff changeset
40 BullentInBoardJungleManager.setJungle(jungle);
38
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
41 }
118
f64ff5bd66f5 Implements persistent for bbs app and Fixed bug JungleUpdater
one
parents: 112
diff changeset
42
f64ff5bd66f5 Implements persistent for bbs app and Fixed bug JungleUpdater
one
parents: 112
diff changeset
43 public void init() throws IOException {
122
cef245f71053 Implemented NetworkJungleBulletinBoard.checkAndCreateLogDirectory method
one
parents: 118
diff changeset
44 checkAndCreateLogDirectory();
118
f64ff5bd66f5 Implements persistent for bbs app and Fixed bug JungleUpdater
one
parents: 112
diff changeset
45 commitLogRecover();
f64ff5bd66f5 Implements persistent for bbs app and Fixed bug JungleUpdater
one
parents: 112
diff changeset
46 }
f64ff5bd66f5 Implements persistent for bbs app and Fixed bug JungleUpdater
one
parents: 112
diff changeset
47
122
cef245f71053 Implemented NetworkJungleBulletinBoard.checkAndCreateLogDirectory method
one
parents: 118
diff changeset
48 public void checkAndCreateLogDirectory() {
cef245f71053 Implemented NetworkJungleBulletinBoard.checkAndCreateLogDirectory method
one
parents: 118
diff changeset
49 File logFile = new File(LOG_DIR);
cef245f71053 Implemented NetworkJungleBulletinBoard.checkAndCreateLogDirectory method
one
parents: 118
diff changeset
50 if(!logFile.exists()) {
cef245f71053 Implemented NetworkJungleBulletinBoard.checkAndCreateLogDirectory method
one
parents: 118
diff changeset
51 logFile.mkdir();
cef245f71053 Implemented NetworkJungleBulletinBoard.checkAndCreateLogDirectory method
one
parents: 118
diff changeset
52 return;
cef245f71053 Implemented NetworkJungleBulletinBoard.checkAndCreateLogDirectory method
one
parents: 118
diff changeset
53 }
cef245f71053 Implemented NetworkJungleBulletinBoard.checkAndCreateLogDirectory method
one
parents: 118
diff changeset
54 if (logFile.isFile()) {
cef245f71053 Implemented NetworkJungleBulletinBoard.checkAndCreateLogDirectory method
one
parents: 118
diff changeset
55 logFile.delete();
cef245f71053 Implemented NetworkJungleBulletinBoard.checkAndCreateLogDirectory method
one
parents: 118
diff changeset
56 logFile.mkdir();
cef245f71053 Implemented NetworkJungleBulletinBoard.checkAndCreateLogDirectory method
one
parents: 118
diff changeset
57 }
cef245f71053 Implemented NetworkJungleBulletinBoard.checkAndCreateLogDirectory method
one
parents: 118
diff changeset
58 }
cef245f71053 Implemented NetworkJungleBulletinBoard.checkAndCreateLogDirectory method
one
parents: 118
diff changeset
59
118
f64ff5bd66f5 Implements persistent for bbs app and Fixed bug JungleUpdater
one
parents: 112
diff changeset
60 public void commitLogRecover() throws IOException {
122
cef245f71053 Implemented NetworkJungleBulletinBoard.checkAndCreateLogDirectory method
one
parents: 118
diff changeset
61 File[] logFiles = new File(LOG_DIR).listFiles();
118
f64ff5bd66f5 Implements persistent for bbs app and Fixed bug JungleUpdater
one
parents: 112
diff changeset
62 for(File logFile : logFiles) {
f64ff5bd66f5 Implements persistent for bbs app and Fixed bug JungleUpdater
one
parents: 112
diff changeset
63 commitLogRecover(logFile);
f64ff5bd66f5 Implements persistent for bbs app and Fixed bug JungleUpdater
one
parents: 112
diff changeset
64 logFile.delete();
f64ff5bd66f5 Implements persistent for bbs app and Fixed bug JungleUpdater
one
parents: 112
diff changeset
65 }
f64ff5bd66f5 Implements persistent for bbs app and Fixed bug JungleUpdater
one
parents: 112
diff changeset
66 if(jungle.getTreeByName("boards") == null) {
f64ff5bd66f5 Implements persistent for bbs app and Fixed bug JungleUpdater
one
parents: 112
diff changeset
67 jungle.createNewTree("boards");
f64ff5bd66f5 Implements persistent for bbs app and Fixed bug JungleUpdater
one
parents: 112
diff changeset
68 }
f64ff5bd66f5 Implements persistent for bbs app and Fixed bug JungleUpdater
one
parents: 112
diff changeset
69 }
f64ff5bd66f5 Implements persistent for bbs app and Fixed bug JungleUpdater
one
parents: 112
diff changeset
70
f64ff5bd66f5 Implements persistent for bbs app and Fixed bug JungleUpdater
one
parents: 112
diff changeset
71 private void commitLogRecover(File logFile) throws IOException {
f64ff5bd66f5 Implements persistent for bbs app and Fixed bug JungleUpdater
one
parents: 112
diff changeset
72 journal.setInputFile(logFile);
f64ff5bd66f5 Implements persistent for bbs app and Fixed bug JungleUpdater
one
parents: 112
diff changeset
73 ChangeListReader reader = journal.getReader();
f64ff5bd66f5 Implements persistent for bbs app and Fixed bug JungleUpdater
one
parents: 112
diff changeset
74 if (reader == null) return;
f64ff5bd66f5 Implements persistent for bbs app and Fixed bug JungleUpdater
one
parents: 112
diff changeset
75 for (ChangeList chList : reader) {
f64ff5bd66f5 Implements persistent for bbs app and Fixed bug JungleUpdater
one
parents: 112
diff changeset
76 String treeName = chList.getTreeName();
f64ff5bd66f5 Implements persistent for bbs app and Fixed bug JungleUpdater
one
parents: 112
diff changeset
77 JungleTree tree = jungle.getTreeByName(treeName);
f64ff5bd66f5 Implements persistent for bbs app and Fixed bug JungleUpdater
one
parents: 112
diff changeset
78 if(tree == null) {
f64ff5bd66f5 Implements persistent for bbs app and Fixed bug JungleUpdater
one
parents: 112
diff changeset
79 tree = jungle.createNewTree(treeName);
f64ff5bd66f5 Implements persistent for bbs app and Fixed bug JungleUpdater
one
parents: 112
diff changeset
80 }
f64ff5bd66f5 Implements persistent for bbs app and Fixed bug JungleUpdater
one
parents: 112
diff changeset
81 JungleTreeEditor editor = tree.getLocalTreeEditor();
f64ff5bd66f5 Implements persistent for bbs app and Fixed bug JungleUpdater
one
parents: 112
diff changeset
82 Either<Error, JungleTreeEditor> either = JungleUpdater.edit(editor, chList);
f64ff5bd66f5 Implements persistent for bbs app and Fixed bug JungleUpdater
one
parents: 112
diff changeset
83 editor = either.b();
f64ff5bd66f5 Implements persistent for bbs app and Fixed bug JungleUpdater
one
parents: 112
diff changeset
84 if(either.isA()) {
f64ff5bd66f5 Implements persistent for bbs app and Fixed bug JungleUpdater
one
parents: 112
diff changeset
85 throw new IOException("Failed commit log recovery");
f64ff5bd66f5 Implements persistent for bbs app and Fixed bug JungleUpdater
one
parents: 112
diff changeset
86 }
f64ff5bd66f5 Implements persistent for bbs app and Fixed bug JungleUpdater
one
parents: 112
diff changeset
87 editor.success();
f64ff5bd66f5 Implements persistent for bbs app and Fixed bug JungleUpdater
one
parents: 112
diff changeset
88 }
f64ff5bd66f5 Implements persistent for bbs app and Fixed bug JungleUpdater
one
parents: 112
diff changeset
89 }
38
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
90
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
91 public Iterable<String> getBoards()
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
92 {
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
93 JungleTree tree = jungle.getTreeByName("boards");
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
94 Node node = tree.getRootNode();
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
95 Children<Node> chs = node.getChildren();
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
96
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
97 IterableConverter.Converter<String,Node> converter = new IterableConverter.Converter<String,Node>(){
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
98 public String conv(Node _b) {
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
99 ByteBuffer e = _b.getAttributes().get("name");
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
100 return new String(e.array());
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
101 }
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
102 };
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
103
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
104 return new IterableConverter<String,Node>(chs,converter);
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
105 }
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
106
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
107 public void createBoards(final String _name,final String _author,final String _initMessage,final String _editKey)
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
108 {
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
109 if(null == jungle.createNewTree(_name)){
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
110 throw new IllegalStateException();
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
111 }
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
112
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
113 JungleTree tree = jungle.getTreeByName("boards");
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
114 JungleTreeEditor editor = tree.getTreeEditor();
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
115 DefaultNodePath root = new DefaultNodePath();
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
116 Either<Error,JungleTreeEditor> either = editor.addNewChildAt(root,0);
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
117 if(either.isA()){
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
118 throw new IllegalStateException();
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
119 }
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
120 editor = either.b();
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
121
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
122 either = editor.putAttribute(root.add(0),"name",ByteBuffer.wrap(_name.getBytes()));
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
123 if(either.isA()){
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
124 throw new IllegalStateException();
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
125 }
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
126 editor = either.b();
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
127 Either<Error,JungleTreeEditor> result = editor.success();
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
128 if(result.isA()){
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
129 throw new IllegalStateException();
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
130 }
56
ccfe9b5e8f11 bug LogUpdateCodeSegment
one
parents: 52
diff changeset
131 final long timestamp = new Date().getTime();
93
75cf01a430a6 Added LocalTreeEditor
one
parents: 92
diff changeset
132
38
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
133
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
134 tree = jungle.getTreeByName(_name);
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
135 editor = tree.getTreeEditor();
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
136 either = editor.addNewChildAt(root,0);
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
137 if(either.isA()){
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
138 throw new IllegalStateException();
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
139 }
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
140 editor = either.b();
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
141
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
142 NodeEditor e = new NodeEditor(){
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
143 public <T extends EditableNode<T>> Either<Error, T> edit(T _e){
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
144 _e = _e.getAttributes().put("author",ByteBuffer.wrap(_author.getBytes())).b();
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
145 _e = _e.getAttributes().put("mes",ByteBuffer.wrap(_initMessage.getBytes())).b();
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
146 _e = _e.getAttributes().put("key",ByteBuffer.wrap(_editKey.getBytes())).b();
56
ccfe9b5e8f11 bug LogUpdateCodeSegment
one
parents: 52
diff changeset
147 ByteBuffer tBuffer = ByteBuffer.allocate(16);
ccfe9b5e8f11 bug LogUpdateCodeSegment
one
parents: 52
diff changeset
148 _e = _e.getAttributes().put("timestamp",tBuffer.putLong(timestamp)).b();
38
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
149 return DefaultEither.newB(_e);
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
150 }
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
151 };
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
152
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
153 either = editor.edit(root.add(0),e);
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
154 if(either.isA()){
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
155 throw new IllegalStateException();
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
156 }
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
157 editor = either.b();
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
158 editor.success();
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
159 }
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
160
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
161 public void createBoardMessage(final String _board,final String _author,final String _message,final String _editKey)
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
162 {
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
163 JungleTree tree = jungle.getTreeByName(_board);
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
164 if(tree == null){
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
165 throw new IllegalStateException();
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
166 }
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
167
46
fd3643699f83 modified pom.xml. fix conflict log4j libraries
one
parents: 45
diff changeset
168 Either<Error, JungleTreeEditor> either;
38
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
169 do{
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
170 Node node = tree.getRootNode();
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
171 int size = node.getChildren().size();
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
172 DefaultNodePath path = new DefaultNodePath();
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
173
65
ebf42371454b remove warning
one
parents: 64
diff changeset
174 JungleTreeEditor editor = tree.getTreeEditor();
46
fd3643699f83 modified pom.xml. fix conflict log4j libraries
one
parents: 45
diff changeset
175 either = editor.addNewChildAt(path,size);
38
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
176 if(either.isA()){
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
177 throw new IllegalStateException();
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
178 }
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
179 editor = either.b();
56
ccfe9b5e8f11 bug LogUpdateCodeSegment
one
parents: 52
diff changeset
180 final long timestamp = new Date().getTime();
38
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
181 NodeEditor e = new NodeEditor(){
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
182 public <T extends EditableNode<T>> Either<Error, T> edit(T _e){
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
183 _e = _e.getAttributes().put("author",ByteBuffer.wrap(_author.getBytes())).b();
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
184 _e = _e.getAttributes().put("mes",ByteBuffer.wrap(_message.getBytes())).b();
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
185 _e = _e.getAttributes().put("key",ByteBuffer.wrap(_editKey.getBytes())).b();
56
ccfe9b5e8f11 bug LogUpdateCodeSegment
one
parents: 52
diff changeset
186 ByteBuffer tBuffer = ByteBuffer.allocate(16);
ccfe9b5e8f11 bug LogUpdateCodeSegment
one
parents: 52
diff changeset
187 _e = _e.getAttributes().put("timestamp",tBuffer.putLong(timestamp)).b();
38
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
188 return DefaultEither.newB(_e);
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
189 }
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
190 };
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
191 path = path.add(size);
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
192 either = editor.edit(path,e);
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
193 if(either.isA()){
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
194 throw new IllegalStateException();
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
195 }
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
196 editor = either.b();
46
fd3643699f83 modified pom.xml. fix conflict log4j libraries
one
parents: 45
diff changeset
197 either = editor.success();
93
75cf01a430a6 Added LocalTreeEditor
one
parents: 92
diff changeset
198
46
fd3643699f83 modified pom.xml. fix conflict log4j libraries
one
parents: 45
diff changeset
199 }while(either.isA());
38
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
200 }
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
201
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
202 public void editMessage(String _board,String _uuid,final String _author,final String _message,final String _editKey)
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
203 {
65
ebf42371454b remove warning
one
parents: 64
diff changeset
204 for(;;) {
38
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
205 DefaultNodePath path = new DefaultNodePath();
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
206 path = path.add(Integer.parseInt(_uuid));
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
207
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
208 JungleTree tree = jungle.getTreeByName(_board);
65
ebf42371454b remove warning
one
parents: 64
diff changeset
209 JungleTreeEditor editor = tree.getTreeEditor();
56
ccfe9b5e8f11 bug LogUpdateCodeSegment
one
parents: 52
diff changeset
210 final long timestamp = new Date().getTime();
38
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
211 NodeEditor e = new NodeEditor(){
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
212 public <T extends EditableNode<T>> Either<Error, T> edit(T _e){
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
213 _e = _e.getAttributes().put("author",ByteBuffer.wrap(_author.getBytes())).b();
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
214 _e = _e.getAttributes().put("mes",ByteBuffer.wrap(_message.getBytes())).b();
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
215 _e = _e.getAttributes().put("key",ByteBuffer.wrap(_editKey.getBytes())).b();
56
ccfe9b5e8f11 bug LogUpdateCodeSegment
one
parents: 52
diff changeset
216 ByteBuffer tBuffer = ByteBuffer.allocate(16);
ccfe9b5e8f11 bug LogUpdateCodeSegment
one
parents: 52
diff changeset
217 _e = _e.getAttributes().put("timestamp",tBuffer.putLong(timestamp)).b();
38
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
218 return DefaultEither.newB(_e);
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
219 }
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
220 };
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
221
65
ebf42371454b remove warning
one
parents: 64
diff changeset
222 Either<Error, JungleTreeEditor> either = editor.edit(path,e);
38
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
223 if(either.isA()){
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
224 throw new IllegalStateException();
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
225 }
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
226 editor = either.b();
46
fd3643699f83 modified pom.xml. fix conflict log4j libraries
one
parents: 45
diff changeset
227 either = editor.success();
65
ebf42371454b remove warning
one
parents: 64
diff changeset
228 if(!either.isA()) {
ebf42371454b remove warning
one
parents: 64
diff changeset
229 return;
ebf42371454b remove warning
one
parents: 64
diff changeset
230 }
ebf42371454b remove warning
one
parents: 64
diff changeset
231 }
ebf42371454b remove warning
one
parents: 64
diff changeset
232
38
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
233 }
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
234
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
235 public Iterable<BoardMessage> getMessages(String _boardName)
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
236 {
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
237 JungleTree tree = jungle.getTreeByName(_boardName);
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
238 Node node = tree.getRootNode();
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
239 Children<Node> chs = node.getChildren();
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
240
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
241 final AtomicInteger counter = new AtomicInteger(0);
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
242 IterableConverter.Converter<BoardMessage,Node> converter = new IterableConverter.Converter<BoardMessage,Node>(){
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
243 public BoardMessage conv(Node _b) {
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
244 String uuid = Integer.toString(counter.get());
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
245 String author = new String(_b.getAttributes().get("author").array());
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
246 String message = new String(_b.getAttributes().get("mes").array());
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
247 counter.incrementAndGet();
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
248 return new BoardMessageImpl(author,message,uuid);
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
249 }
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
250 };
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
251 return new IterableConverter<BoardMessage,Node>(chs,converter);
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
252 }
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
253
46
fd3643699f83 modified pom.xml. fix conflict log4j libraries
one
parents: 45
diff changeset
254
fd3643699f83 modified pom.xml. fix conflict log4j libraries
one
parents: 45
diff changeset
255
38
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
256 private static class BoardMessageImpl implements BoardMessage
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
257 {
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
258 private final String author;
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
259 private final String message;
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
260 private final String uuid;
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
261
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
262 public BoardMessageImpl(String _author,String _message,String _uuid)
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
263 {
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
264 author = _author;
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
265 message = _message;
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
266 uuid = _uuid;
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
267 }
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
268
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
269 public String getAuthor()
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
270 {
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
271 return author;
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
272 }
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
273
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
274 public String getMessage()
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
275 {
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
276 return message;
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
277 }
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
278
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
279 public String getUUID()
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
280 {
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
281 return uuid;
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
282 }
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
283 }
d8ee57a1c2c6 add pom.xml and bbs
one
parents:
diff changeset
284 }