Mercurial > hg > Members > nobuyasu > jungle-network
annotate src/main/java/app/bbs/NetworkJungleBulletinBoard.java @ 168:91f6dd655a01
refactoring
author | tatsuki |
---|---|
date | Tue, 29 Jul 2014 10:58:11 +0900 |
parents | 564f683b7aef |
children | 54d68cda84c4 |
rev | line source |
---|---|
112 | 1 package app.bbs; |
38 | 2 |
135
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
3 import java.io.File; |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
4 import java.io.IOException; |
38 | 5 import java.nio.ByteBuffer; |
6 import java.util.concurrent.atomic.AtomicInteger; | |
39 | 7 |
8 import alice.jungle.core.NetworkDefaultJungle; | |
124 | 9 import alice.jungle.persistent.AliceJournal; |
135
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
10 import alice.jungle.persistent.NetworkJournal; |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
11 import alice.jungle.persistent.PersistentJournal; |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
12 import alice.jungle.transaction.JungleUpdater; |
167 | 13 import app.bbs.thinks.getAttributeImp; |
38 | 14 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.Jungle; |
15 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTree; | |
16 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTreeEditor; | |
125 | 17 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.bbs.BoardMessage; |
38 | 18 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.core.Children; |
19 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.core.Node; | |
135
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
20 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.persistent.ChangeList; |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
21 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.persistent.ChangeListReader; |
38 | 22 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultNodePath; |
23 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultTreeEditor; | |
166 | 24 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.TraversableNodeWrapper; |
38 | 25 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.EditableNode; |
26 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.NodeEditor; | |
166 | 27 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser.DefaultEvaluator; |
38 | 28 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser.DefaultTraverser; |
166 | 29 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser.Traversal; |
38 | 30 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.DefaultEither; |
31 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Either; | |
32 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error; | |
33 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.IterableConverter; | |
166 | 34 import junit.framework.Assert; |
38 | 35 |
166 | 36 public class NetworkJungleBulletinBoard implements NetworkBulletinBoard { |
38 | 37 private final Jungle jungle; |
135
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
38 private final NetworkJournal journal; |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
39 private final String LOG_DIR; |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
40 private Boolean persistentFlag; |
166 | 41 private AtomicInteger requestCounter; |
150
eef85b5faa15
Removed renew atribute
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
149
diff
changeset
|
42 private long renewTime; |
166 | 43 |
44 private NetworkJungleBulletinBoard(String _uuid, NetworkJournal _journal) { | |
135
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
45 journal = _journal; |
168 | 46 jungle = new NetworkDefaultJungle(journal, _uuid, new DefaultTreeEditor(new DefaultTraverser())); |
135
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
47 BulletinBoardJungleManager.setJungle(jungle); |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
48 persistentFlag = false; |
147
202ea963ec20
Added requestCounter in BulletinBoardJungleManager
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
146
diff
changeset
|
49 requestCounter = BulletinBoardJungleManager.getRequestCounter(); |
135
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
50 LOG_DIR = "./log"; |
150
eef85b5faa15
Removed renew atribute
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
149
diff
changeset
|
51 renewTime = 0; |
135
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
52 } |
166 | 53 |
54 public NetworkJungleBulletinBoard(String _uuid) { | |
135
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
55 this(_uuid, new AliceJournal()); |
124 | 56 jungle.createNewTree("boards"); |
135
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
57 } |
166 | 58 |
59 public static NetworkBulletinBoard NewPersistentJungle(String _uuid) { | |
168 | 60 NetworkJungleBulletinBoard board = new NetworkJungleBulletinBoard( _uuid, new PersistentJournal()); |
135
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
61 board.persistentFlag = true; |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
62 return board; |
38 | 63 } |
166 | 64 |
124 | 65 public void init() { |
166 | 66 if (!persistentFlag) { |
135
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
67 return; |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
68 } |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
69 checkAndCreateLogDirectory(); |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
70 try { |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
71 commitLogRecover(); |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
72 } catch (IOException e) { |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
73 e.printStackTrace(); |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
74 } |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
75 } |
166 | 76 |
135
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
77 public void checkAndCreateLogDirectory() { |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
78 File logFile = new File(LOG_DIR); |
166 | 79 if (!logFile.exists()) { |
135
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
80 logFile.mkdir(); |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
81 return; |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
82 } |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
83 if (logFile.isFile()) { |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
84 logFile.delete(); |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
85 logFile.mkdir(); |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
86 } |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
87 } |
166 | 88 |
135
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
89 public void commitLogRecover() throws IOException { |
166 | 90 File[] logFiles = new File(LOG_DIR).listFiles(); |
91 for (File logFile : logFiles) { | |
135
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
92 commitLogRecover(logFile); |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
93 logFile.delete(); |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
94 } |
166 | 95 if (jungle.getTreeByName("boards") == null) { |
135
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
96 jungle.createNewTree("boards"); |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
97 } |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
98 } |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
99 |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
100 private void commitLogRecover(File logFile) throws IOException { |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
101 journal.setInputFile(logFile); |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
102 ChangeListReader reader = journal.getReader(); |
166 | 103 if (reader == null) |
104 return; | |
135
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
105 for (ChangeList chList : reader) { |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
106 String treeName = chList.getTreeName(); |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
107 JungleTree tree = jungle.getTreeByName(treeName); |
166 | 108 if (tree == null) { |
135
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
109 tree = jungle.createNewTree(treeName); |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
110 } |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
111 JungleTreeEditor editor = tree.getLocalTreeEditor(); |
168 | 112 Either<Error, JungleTreeEditor> either = JungleUpdater.edit(editor, chList); |
135
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
113 editor = either.b(); |
166 | 114 if (either.isA()) { |
135
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
115 throw new IOException("Failed commit log recovery"); |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
116 } |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
117 editor.success(); |
2e8034524259
Added NetworkJournal and SingletonMessageFromAlice class
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
118 } |
118
f64ff5bd66f5
Implements persistent for bbs app and Fixed bug JungleUpdater
one
parents:
112
diff
changeset
|
119 } |
38 | 120 |
166 | 121 public Iterable<String> getBoards() { |
38 | 122 JungleTree tree = jungle.getTreeByName("boards"); |
123 Node node = tree.getRootNode(); | |
124 Children<Node> chs = node.getChildren(); | |
166 | 125 |
126 IterableConverter.Converter<String, Node> converter = new IterableConverter.Converter<String, Node>() { | |
38 | 127 public String conv(Node _b) { |
128 ByteBuffer e = _b.getAttributes().get("name"); | |
129 return new String(e.array()); | |
130 } | |
131 }; | |
166 | 132 |
133 return new IterableConverter<String, Node>(chs, converter); | |
38 | 134 } |
166 | 135 |
146
29734d7d6521
Added ShowMessageWithTimeStampServlet
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
143
diff
changeset
|
136 public long getRenewTime(String _boardName) { |
150
eef85b5faa15
Removed renew atribute
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
149
diff
changeset
|
137 return renewTime; |
146
29734d7d6521
Added ShowMessageWithTimeStampServlet
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
143
diff
changeset
|
138 } |
38 | 139 |
168 | 140 public void createBoards(final String _name, final String _author, final String _initMessage, final String _editKey) { |
143
7a7bac51a3cc
Added RequestNumCheckServlet
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
139
diff
changeset
|
141 requestCounter.incrementAndGet(); |
166 | 142 if (null == jungle.createNewTree(_name)) { |
38 | 143 throw new IllegalStateException(); |
144 } | |
166 | 145 |
38 | 146 JungleTree tree = jungle.getTreeByName("boards"); |
147 JungleTreeEditor editor = tree.getTreeEditor(); | |
148 DefaultNodePath root = new DefaultNodePath(); | |
166 | 149 Either<Error, JungleTreeEditor> either = editor.addNewChildAt(root, 0); |
150 if (either.isA()) { | |
38 | 151 throw new IllegalStateException(); |
152 } | |
153 editor = either.b(); | |
166 | 154 |
168 | 155 either = editor.putAttribute(root.add(0), "name",ByteBuffer.wrap(_name.getBytes())); |
166 | 156 if (either.isA()) { |
38 | 157 throw new IllegalStateException(); |
158 } | |
159 editor = either.b(); | |
149
06cddf529325
Modified lates renew time
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
147
diff
changeset
|
160 final long timestamp = System.currentTimeMillis(); |
129
1a3edba05f50
Fixed bug NetworkJungleBulletinBoard
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
126
diff
changeset
|
161 ByteBuffer tBuffer = ByteBuffer.allocate(16); |
146
29734d7d6521
Added ShowMessageWithTimeStampServlet
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
143
diff
changeset
|
162 tBuffer.putLong(timestamp); |
166 | 163 either = editor.putAttribute(root.add(0), "timestamp", tBuffer); |
164 if (either.isA()) { | |
129
1a3edba05f50
Fixed bug NetworkJungleBulletinBoard
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
126
diff
changeset
|
165 throw new IllegalStateException(); |
1a3edba05f50
Fixed bug NetworkJungleBulletinBoard
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
126
diff
changeset
|
166 } |
146
29734d7d6521
Added ShowMessageWithTimeStampServlet
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
143
diff
changeset
|
167 either = either.b().success(); |
166 | 168 if (either.isA()) { |
38 | 169 throw new IllegalStateException(); |
170 } | |
166 | 171 |
38 | 172 tree = jungle.getTreeByName(_name); |
173 editor = tree.getTreeEditor(); | |
166 | 174 either = editor.addNewChildAt(root, 0); |
175 if (either.isA()) { | |
38 | 176 throw new IllegalStateException(); |
177 } | |
178 editor = either.b(); | |
166 | 179 |
180 NodeEditor e = new NodeEditor() { | |
181 public <T extends EditableNode<T>> Either<Error, T> edit(T _e) { | |
168 | 182 _e = _e.getAttributes().put("author", ByteBuffer.wrap(_author.getBytes())).b(); |
183 _e = _e.getAttributes().put("mes", ByteBuffer.wrap(_initMessage.getBytes())).b(); | |
184 _e = _e.getAttributes().put("key", ByteBuffer.wrap(_editKey.getBytes())).b(); | |
130 | 185 ByteBuffer tBuffer2 = ByteBuffer.allocate(16); |
146
29734d7d6521
Added ShowMessageWithTimeStampServlet
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
143
diff
changeset
|
186 tBuffer2.putLong(timestamp); |
166 | 187 _e = _e.getAttributes().put("timestamp", tBuffer2).b(); |
38 | 188 return DefaultEither.newB(_e); |
189 } | |
190 }; | |
166 | 191 |
192 either = editor.edit(root.add(0), e); | |
193 if (either.isA()) { | |
38 | 194 throw new IllegalStateException(); |
195 } | |
146
29734d7d6521
Added ShowMessageWithTimeStampServlet
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
143
diff
changeset
|
196 either.b().success(); |
147
202ea963ec20
Added requestCounter in BulletinBoardJungleManager
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
146
diff
changeset
|
197 |
38 | 198 } |
199 | |
168 | 200 public void createFolder(final String _board, final String _author, final String _message, final String _editKey, String _nodeNum) { |
38 | 201 JungleTree tree = jungle.getTreeByName(_board); |
166 | 202 if (tree == null) { |
38 | 203 throw new IllegalStateException(); |
204 } | |
167 | 205 |
166 | 206 DefaultNodePath path = new DefaultNodePath(); |
207 try { | |
208 for (int count = 0; _nodeNum.substring(count, count + 1) != null; count++) { | |
209 if (!_nodeNum.substring(count, count + 1).equals("/")) | |
168 | 210 path = path.add(Integer.parseInt(_nodeNum.substring(count, count + 1))); |
166 | 211 } |
212 } catch (Exception _e) { | |
213 } | |
214 requestCounter.incrementAndGet(); | |
46 | 215 Either<Error, JungleTreeEditor> either; |
149
06cddf529325
Modified lates renew time
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
147
diff
changeset
|
216 final long timestamp = System.currentTimeMillis(); |
146
29734d7d6521
Added ShowMessageWithTimeStampServlet
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
143
diff
changeset
|
217 final ByteBuffer tBuffer = ByteBuffer.allocate(16); |
29734d7d6521
Added ShowMessageWithTimeStampServlet
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
143
diff
changeset
|
218 tBuffer.putLong(timestamp); |
168 | 219 |
166 | 220 do { |
38 | 221 Node node = tree.getRootNode(); |
166 | 222 DefaultTraverser traverser = new DefaultTraverser(); |
168 | 223 TraversableNodeWrapper<Node> traversable = new TraversableNodeWrapper<Node>(node); |
166 | 224 DefaultEvaluator evaluator = new DefaultEvaluator(path); |
168 | 225 Either<Error, Traversal<TraversableNodeWrapper<Node>>> ret = traverser.traverse(traversable, evaluator); |
166 | 226 if (ret.isA()) { |
227 Assert.fail(); | |
228 } | |
229 | |
230 Traversal<TraversableNodeWrapper<Node>> traversal = ret.b(); | |
231 TraversableNodeWrapper<Node> target = traversal.destination(); | |
167 | 232 int size = target.getWrapped().getChildren().size(); |
65 | 233 JungleTreeEditor editor = tree.getTreeEditor(); |
166 | 234 either = editor.addNewChildAt(path, size); |
235 if (either.isA()) { | |
38 | 236 throw new IllegalStateException(); |
237 } | |
238 editor = either.b(); | |
146
29734d7d6521
Added ShowMessageWithTimeStampServlet
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
143
diff
changeset
|
239 |
166 | 240 NodeEditor e = new NodeEditor() { |
241 public <T extends EditableNode<T>> Either<Error, T> edit(T _e) { | |
168 | 242 _e = _e.getAttributes().put("mes", ByteBuffer.wrap(_message.getBytes())).b(); |
166 | 243 _e = _e.getAttributes().put("timestamp", tBuffer).b(); |
38 | 244 return DefaultEither.newB(_e); |
245 } | |
246 }; | |
247 path = path.add(size); | |
166 | 248 either = editor.edit(path, e); |
249 if (either.isA()) { | |
38 | 250 throw new IllegalStateException(); |
251 } | |
252 editor = either.b(); | |
46 | 253 either = editor.success(); |
166 | 254 } while (either.isA()); |
93 | 255 |
38 | 256 } |
257 | |
168 | 258 public void createBoardMessage(final String _board, final String _author, final String _message, final String _editKey) { |
166 | 259 requestCounter.incrementAndGet(); |
260 JungleTree tree = jungle.getTreeByName(_board); | |
261 if (tree == null) { | |
262 throw new IllegalStateException(); | |
263 } | |
264 | |
265 Either<Error, JungleTreeEditor> either; | |
266 final long timestamp = System.currentTimeMillis(); | |
267 final ByteBuffer tBuffer = ByteBuffer.allocate(16); | |
268 tBuffer.putLong(timestamp); | |
269 do { | |
168 | 270 |
166 | 271 Node node = tree.getRootNode(); |
272 int size = node.getChildren().size(); | |
273 DefaultNodePath path = new DefaultNodePath(); | |
274 | |
275 JungleTreeEditor editor = tree.getTreeEditor(); | |
276 either = editor.addNewChildAt(path, size); | |
277 if (either.isA()) { | |
278 throw new IllegalStateException(); | |
279 } | |
280 editor = either.b(); | |
281 | |
282 NodeEditor e = new NodeEditor() { | |
283 public <T extends EditableNode<T>> Either<Error, T> edit(T _e) { | |
168 | 284 _e = _e.getAttributes().put("author", ByteBuffer.wrap(_author.getBytes())).b(); |
285 _e = _e.getAttributes().put("mes", ByteBuffer.wrap(_message.getBytes())).b(); | |
286 _e = _e.getAttributes().put("key", ByteBuffer.wrap(_editKey.getBytes())).b(); | |
166 | 287 _e = _e.getAttributes().put("timestamp", tBuffer).b(); |
288 return DefaultEither.newB(_e); | |
289 } | |
290 }; | |
291 path = path.add(size); | |
292 either = editor.edit(path, e); | |
293 if (either.isA()) { | |
294 throw new IllegalStateException(); | |
295 } | |
296 editor = either.b(); | |
297 either = editor.success(); | |
298 } while (either.isA()); | |
299 | |
300 } | |
301 | |
168 | 302 public void editMessage(String _board, String _uuid, final String _author, final String _message, final String _editKey) { |
143
7a7bac51a3cc
Added RequestNumCheckServlet
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
139
diff
changeset
|
303 requestCounter.incrementAndGet(); |
149
06cddf529325
Modified lates renew time
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
147
diff
changeset
|
304 final long timestamp = System.currentTimeMillis(); |
146
29734d7d6521
Added ShowMessageWithTimeStampServlet
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
143
diff
changeset
|
305 final ByteBuffer tBuffer = ByteBuffer.allocate(16); |
29734d7d6521
Added ShowMessageWithTimeStampServlet
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
143
diff
changeset
|
306 tBuffer.putLong(timestamp); |
29734d7d6521
Added ShowMessageWithTimeStampServlet
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
143
diff
changeset
|
307 JungleTree tree = jungle.getTreeByName(_board); |
29734d7d6521
Added ShowMessageWithTimeStampServlet
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
143
diff
changeset
|
308 Either<Error, JungleTreeEditor> either = null; |
168 | 309 |
146
29734d7d6521
Added ShowMessageWithTimeStampServlet
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
143
diff
changeset
|
310 do { |
38 | 311 DefaultNodePath path = new DefaultNodePath(); |
312 path = path.add(Integer.parseInt(_uuid)); | |
146
29734d7d6521
Added ShowMessageWithTimeStampServlet
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
143
diff
changeset
|
313 |
65 | 314 JungleTreeEditor editor = tree.getTreeEditor(); |
166 | 315 NodeEditor e = new NodeEditor() { |
316 public <T extends EditableNode<T>> Either<Error, T> edit(T _e) { | |
168 | 317 _e = _e.getAttributes().put("author", ByteBuffer.wrap(_author.getBytes())).b(); |
318 _e = _e.getAttributes().put("mes", ByteBuffer.wrap(_message.getBytes())).b(); | |
319 _e = _e.getAttributes().put("key", ByteBuffer.wrap(_editKey.getBytes())).b(); | |
166 | 320 _e = _e.getAttributes().put("timestamp", tBuffer).b(); |
321 return DefaultEither.newB(_e); | |
38 | 322 } |
323 }; | |
166 | 324 either = editor.edit(path, e); |
325 if (either.isA()) { | |
38 | 326 throw new IllegalStateException(); |
327 } | |
328 editor = either.b(); | |
46 | 329 either = editor.success(); |
166 | 330 } while (either.isA()); |
331 renewTime = timestamp; | |
332 } | |
167 | 333 |
168 | 334 public void createAttribute(String _board, String _uuid, final String _author, final String _message, final String _editKey) { |
166 | 335 requestCounter.incrementAndGet(); |
336 final long timestamp = System.currentTimeMillis(); | |
337 final ByteBuffer tBuffer = ByteBuffer.allocate(16); | |
338 tBuffer.putLong(timestamp); | |
339 JungleTree tree = jungle.getTreeByName(_board); | |
340 Either<Error, JungleTreeEditor> either = null; | |
341 DefaultNodePath path = new DefaultNodePath(); | |
342 do { | |
343 try { | |
344 for (int count = 0; _uuid.substring(count, count + 1) != null; count++) { | |
345 if (!_uuid.substring(count, count + 1).equals("/")) | |
168 | 346 path = path.add(Integer.parseInt(_uuid.substring(count, count + 1))); |
166 | 347 } |
348 } catch (Exception _e) { | |
349 } | |
168 | 350 |
166 | 351 JungleTreeEditor editor = tree.getTreeEditor(); |
352 NodeEditor e = new NodeEditor() { | |
353 public <T extends EditableNode<T>> Either<Error, T> edit(T _e) { | |
167 | 354 // EnableNodeWrapper<T> node = _e.getWrap(); |
355 String str = "0"; | |
356 int count = 0; | |
168 | 357 for (; _e.getAttributes().get("mes" + String.valueOf(count)) != null; count++) { |
167 | 358 } |
359 str = String.valueOf(count); | |
360 _e = _e.getAttributes().put("mes" + str, ByteBuffer.wrap(_message.getBytes())).b(); | |
361 _e = _e.getAttributes().put("timestamp" + str, tBuffer).b(); | |
166 | 362 return DefaultEither.newB(_e); |
363 } | |
364 }; | |
365 either = editor.edit(path, e); | |
366 if (either.isA()) { | |
367 throw new IllegalStateException(); | |
368 } | |
369 editor = either.b(); | |
370 either = editor.success(); | |
371 } while (either.isA()); | |
372 } | |
167 | 373 |
168 | 374 public void editMatrixMessage(String _board, String _uuid, final String _author, final String _message, final String _editKey) { |
166 | 375 requestCounter.incrementAndGet(); |
376 final long timestamp = System.currentTimeMillis(); | |
377 final ByteBuffer tBuffer = ByteBuffer.allocate(16); | |
378 tBuffer.putLong(timestamp); | |
379 JungleTree tree = jungle.getTreeByName(_board); | |
380 Either<Error, JungleTreeEditor> either = null; | |
381 do { | |
382 DefaultNodePath path = new DefaultNodePath(); | |
383 path = path.add(Integer.parseInt(_uuid)); | |
384 | |
385 JungleTreeEditor editor = tree.getTreeEditor(); | |
386 NodeEditor e = new NodeEditor() { | |
387 public <T extends EditableNode<T>> Either<Error, T> edit(T _e) { | |
168 | 388 _e = _e.getAttributes().put("author", ByteBuffer.wrap(_author.getBytes())).b(); |
389 _e = _e.getAttributes().put("mes", ByteBuffer.wrap(_message.getBytes())).b(); | |
390 _e = _e.getAttributes().put("key", ByteBuffer.wrap(_editKey.getBytes())).b(); | |
166 | 391 _e = _e.getAttributes().put("timestamp", tBuffer).b(); |
392 return DefaultEither.newB(_e); | |
393 } | |
394 }; | |
395 either = editor.edit(path, e); | |
396 if (either.isA()) { | |
397 throw new IllegalStateException(); | |
398 } | |
399 editor = either.b(); | |
400 either = editor.success(); | |
401 } while (either.isA()); | |
150
eef85b5faa15
Removed renew atribute
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
149
diff
changeset
|
402 renewTime = timestamp; |
38 | 403 } |
404 | |
166 | 405 public Iterable<BoardMessage> getMessages(String _boardName) { |
143
7a7bac51a3cc
Added RequestNumCheckServlet
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
139
diff
changeset
|
406 requestCounter.incrementAndGet(); |
38 | 407 JungleTree tree = jungle.getTreeByName(_boardName); |
408 Node node = tree.getRootNode(); | |
409 Children<Node> chs = node.getChildren(); | |
410 final AtomicInteger counter = new AtomicInteger(0); | |
166 | 411 IterableConverter.Converter<BoardMessage, Node> converter = new IterableConverter.Converter<BoardMessage, Node>() { |
412 public BoardMessage conv(Node _b) { | |
413 String uuid = Integer.toString(counter.get()); | |
168 | 414 String author = new String(_b.getAttributes().get("author") .array()); |
415 String message = new String(_b.getAttributes().get("mes").array()); | |
166 | 416 counter.incrementAndGet(); |
417 return new BoardMessageImpl(author, message, uuid); | |
418 } | |
419 }; | |
420 return new IterableConverter<BoardMessage, Node>(chs, converter); | |
421 } | |
422 | |
423 public Iterable<BoardMessage> getFolder(String _boardName, String _nodeNum) { | |
167 | 424 |
166 | 425 DefaultNodePath path = new DefaultNodePath(); |
426 try { | |
427 for (int count = 0; _nodeNum.substring(count, count + 1) != null; count++) { | |
428 if (!_nodeNum.substring(count, count + 1).equals("/")) | |
168 | 429 path = path.add(Integer.parseInt(_nodeNum.substring(count,count + 1))); |
166 | 430 } |
431 } catch (Exception _e) { | |
432 } | |
433 requestCounter.incrementAndGet(); | |
434 JungleTree tree = jungle.getTreeByName(_boardName); | |
435 Node node = tree.getRootNode(); | |
436 | |
437 DefaultTraverser traverser = new DefaultTraverser(); | |
168 | 438 TraversableNodeWrapper<Node> traversable = new TraversableNodeWrapper<Node>(node); |
166 | 439 DefaultEvaluator evaluator = new DefaultEvaluator(path); |
168 | 440 Either<Error, Traversal<TraversableNodeWrapper<Node>>> ret = traverser.traverse(traversable, evaluator); |
166 | 441 if (ret.isA()) { |
442 Assert.fail(); | |
443 } | |
444 | |
445 Traversal<TraversableNodeWrapper<Node>> traversal = ret.b(); | |
446 TraversableNodeWrapper<Node> target = traversal.destination(); | |
447 Children<Node> chs = target.getWrapped().getChildren(); | |
448 | |
449 final AtomicInteger counter = new AtomicInteger(0); | |
450 IterableConverter.Converter<BoardMessage, Node> converter = new IterableConverter.Converter<BoardMessage, Node>() { | |
38 | 451 public BoardMessage conv(Node _b) { |
452 String uuid = Integer.toString(counter.get()); | |
453 String message = new String(_b.getAttributes().get("mes").array()); | |
454 counter.incrementAndGet(); | |
168 | 455 return new BoardMessageImpl(null, message, uuid); |
38 | 456 } |
457 }; | |
166 | 458 return new IterableConverter<BoardMessage, Node>(chs, converter); |
38 | 459 } |
167 | 460 |
461 | |
462 public getAttributeImp getAttribute(String _boardName, String _nodeNum) { | |
463 | |
464 DefaultNodePath path = new DefaultNodePath(); | |
465 try { | |
466 for (int count = 0; _nodeNum.substring(count, count + 1) != null; count++) { | |
467 if (!_nodeNum.substring(count, count + 1).equals("/")) | |
168 | 468 path = path.add(Integer.parseInt(_nodeNum.substring(count,count + 1))); |
167 | 469 } |
470 } catch (Exception _e) { | |
471 } | |
472 requestCounter.incrementAndGet(); | |
473 JungleTree tree = jungle.getTreeByName(_boardName); | |
474 Node node = tree.getRootNode(); | |
475 | |
476 DefaultTraverser traverser = new DefaultTraverser(); | |
477 TraversableNodeWrapper<Node> traversable = new TraversableNodeWrapper<Node>(node); | |
478 DefaultEvaluator evaluator = new DefaultEvaluator(path); | |
479 Either<Error, Traversal<TraversableNodeWrapper<Node>>> ret = traverser.traverse(traversable, evaluator); | |
480 if (ret.isA()) { | |
481 Assert.fail(); | |
482 } | |
483 | |
484 Traversal<TraversableNodeWrapper<Node>> traversal = ret.b(); | |
485 TraversableNodeWrapper<Node> target = traversal.destination(); | |
486 return new getAttributeImp(target.getWrapped()); | |
487 } | |
166 | 488 |
143
7a7bac51a3cc
Added RequestNumCheckServlet
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
139
diff
changeset
|
489 public int getRequestNum() { |
7a7bac51a3cc
Added RequestNumCheckServlet
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
139
diff
changeset
|
490 return requestCounter.get(); |
7a7bac51a3cc
Added RequestNumCheckServlet
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
139
diff
changeset
|
491 } |
146
29734d7d6521
Added ShowMessageWithTimeStampServlet
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
143
diff
changeset
|
492 |
166 | 493 private static class BoardMessageImpl implements BoardMessage { |
38 | 494 private final String author; |
495 private final String message; | |
496 private final String uuid; | |
166 | 497 |
498 public BoardMessageImpl(String _author, String _message, String _uuid) { | |
38 | 499 author = _author; |
500 message = _message; | |
501 uuid = _uuid; | |
502 } | |
503 | |
166 | 504 public String getAuthor() { |
38 | 505 return author; |
506 } | |
507 | |
166 | 508 public String getMessage() { |
38 | 509 return message; |
510 } | |
511 | |
166 | 512 public String getUUID() { |
38 | 513 return uuid; |
514 } | |
143
7a7bac51a3cc
Added RequestNumCheckServlet
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
139
diff
changeset
|
515 |
38 | 516 } |
124 | 517 |
38 | 518 } |