Mercurial > hg > Members > nobuyasu > jungle-network
annotate src/main/java/app/bbs/NetworkJungleBulletinBoard.java @ 170:2403b9a4416f
create Delete Attribute Method
author | tatsuki |
---|---|
date | Wed, 30 Jul 2014 10:20:01 +0900 |
parents | 54d68cda84c4 |
children | df063cf6f3b5 |
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 | |
169 | 302 public void editMessage(String _board, String _nodeNum, 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(); |
169 | 312 try { |
313 for (int count = 0; _nodeNum.substring(count, count + 1) != null; count++) { | |
314 if (!_nodeNum.substring(count, count + 1).equals("/")) | |
315 path = path.add(Integer.parseInt(_nodeNum.substring(count,count + 1))); | |
316 } | |
317 } catch (Exception _e) { | |
318 } | |
65 | 319 JungleTreeEditor editor = tree.getTreeEditor(); |
166 | 320 NodeEditor e = new NodeEditor() { |
321 public <T extends EditableNode<T>> Either<Error, T> edit(T _e) { | |
168 | 322 _e = _e.getAttributes().put("author", ByteBuffer.wrap(_author.getBytes())).b(); |
323 _e = _e.getAttributes().put("mes", ByteBuffer.wrap(_message.getBytes())).b(); | |
324 _e = _e.getAttributes().put("key", ByteBuffer.wrap(_editKey.getBytes())).b(); | |
166 | 325 _e = _e.getAttributes().put("timestamp", tBuffer).b(); |
326 return DefaultEither.newB(_e); | |
38 | 327 } |
328 }; | |
166 | 329 either = editor.edit(path, e); |
330 if (either.isA()) { | |
38 | 331 throw new IllegalStateException(); |
332 } | |
333 editor = either.b(); | |
46 | 334 either = editor.success(); |
166 | 335 } while (either.isA()); |
336 renewTime = timestamp; | |
337 } | |
167 | 338 |
168 | 339 public void createAttribute(String _board, String _uuid, final String _author, final String _message, final String _editKey) { |
166 | 340 requestCounter.incrementAndGet(); |
341 final long timestamp = System.currentTimeMillis(); | |
342 final ByteBuffer tBuffer = ByteBuffer.allocate(16); | |
343 tBuffer.putLong(timestamp); | |
344 JungleTree tree = jungle.getTreeByName(_board); | |
345 Either<Error, JungleTreeEditor> either = null; | |
346 DefaultNodePath path = new DefaultNodePath(); | |
347 do { | |
348 try { | |
349 for (int count = 0; _uuid.substring(count, count + 1) != null; count++) { | |
350 if (!_uuid.substring(count, count + 1).equals("/")) | |
168 | 351 path = path.add(Integer.parseInt(_uuid.substring(count, count + 1))); |
166 | 352 } |
353 } catch (Exception _e) { | |
354 } | |
168 | 355 |
166 | 356 JungleTreeEditor editor = tree.getTreeEditor(); |
357 NodeEditor e = new NodeEditor() { | |
358 public <T extends EditableNode<T>> Either<Error, T> edit(T _e) { | |
167 | 359 String str = "0"; |
360 int count = 0; | |
168 | 361 for (; _e.getAttributes().get("mes" + String.valueOf(count)) != null; count++) { |
167 | 362 } |
169 | 363 str = String.valueOf(count); |
167 | 364 _e = _e.getAttributes().put("mes" + str, ByteBuffer.wrap(_message.getBytes())).b(); |
365 _e = _e.getAttributes().put("timestamp" + str, tBuffer).b(); | |
166 | 366 return DefaultEither.newB(_e); |
367 } | |
368 }; | |
369 either = editor.edit(path, e); | |
370 if (either.isA()) { | |
371 throw new IllegalStateException(); | |
372 } | |
373 editor = either.b(); | |
374 either = editor.success(); | |
375 } while (either.isA()); | |
376 } | |
167 | 377 |
169 | 378 public void editAttribute(String boardName, String _path, String id, String _message) { |
379 requestCounter.incrementAndGet(); | |
380 final long timestamp = System.currentTimeMillis(); | |
381 final ByteBuffer tBuffer = ByteBuffer.allocate(16); | |
382 tBuffer.putLong(timestamp); | |
383 JungleTree tree = jungle.getTreeByName(boardName); | |
384 Either<Error, JungleTreeEditor> either = null; | |
385 DefaultNodePath path = new DefaultNodePath(); | |
386 do { | |
387 try { | |
388 for (int count = 0; _path.substring(count, count + 1) != null; count++) { | |
389 if (!_path.substring(count, count + 1).equals("/")) | |
390 path = path.add(Integer.parseInt(_path.substring(count, count + 1))); | |
391 } | |
392 } catch (Exception _e) { | |
393 } | |
394 | |
395 JungleTreeEditor editor = tree.getTreeEditor(); | |
396 NodeEditor e = new NodeEditor() { | |
397 public <T extends EditableNode<T>> Either<Error, T> edit(T _e) { | |
398 // EnableNodeWrapper<T> node = _e.getWrap(); | |
399 _e = _e.getAttributes().put("mes" + id, ByteBuffer.wrap(_message.getBytes())).b(); | |
400 _e = _e.getAttributes().put("timestamp" + id, tBuffer).b(); | |
401 return DefaultEither.newB(_e); | |
402 } | |
403 }; | |
404 either = editor.edit(path, e); | |
405 if (either.isA()) { | |
406 throw new IllegalStateException(); | |
407 } | |
408 editor = either.b(); | |
409 either = editor.success(); | |
410 } while (either.isA()); | |
411 } | |
412 | |
170 | 413 public void deleteAttribute(String _board, String _path ,final String id) { |
414 requestCounter.incrementAndGet(); | |
415 final long timestamp = System.currentTimeMillis(); | |
416 final ByteBuffer tBuffer = ByteBuffer.allocate(16); | |
417 tBuffer.putLong(timestamp); | |
418 JungleTree tree = jungle.getTreeByName(_board); | |
419 Either<Error, JungleTreeEditor> either = null; | |
420 DefaultNodePath path = new DefaultNodePath(); | |
421 do { | |
422 try { | |
423 for (int count = 0; _path.substring(count, count + 1) != null; count++) { | |
424 if (!_path.substring(count, count + 1).equals("/")) | |
425 path = path.add(Integer.parseInt(_path.substring(count, count + 1))); | |
426 } | |
427 } catch (Exception _e) { | |
428 } | |
429 | |
430 JungleTreeEditor editor = tree.getTreeEditor(); | |
431 NodeEditor e = new NodeEditor() { | |
432 public <T extends EditableNode<T>> Either<Error, T> edit(T _e) { | |
433 _e = _e.getAttributes().delete("mes" + id ).b(); | |
434 _e = _e.getAttributes().delete("timestamp" + id ).b(); | |
435 int count = Integer.parseInt(id); | |
436 for (; _e.getAttributes().get("mes" + String.valueOf(count + 1)) != null; count++) { | |
437 _e = _e.getAttributes().put("mes" + count, _e.getAttributes().get("mes" + String.valueOf(count + 1))).b(); | |
438 _e = _e.getAttributes().put("timestamp" + count, tBuffer).b(); | |
439 } | |
440 _e = _e.getAttributes().delete("mes" + count ).b(); | |
441 _e = _e.getAttributes().delete("timestamp" + count ).b(); | |
442 return DefaultEither.newB(_e); | |
443 } | |
444 }; | |
445 either = editor.edit(path, e); | |
446 if (either.isA()) { | |
447 throw new IllegalStateException(); | |
448 } | |
449 editor = either.b(); | |
450 either = editor.success(); | |
451 } while (either.isA()); | |
452 } | |
453 | |
454 | |
169 | 455 |
168 | 456 public void editMatrixMessage(String _board, String _uuid, final String _author, final String _message, final String _editKey) { |
166 | 457 requestCounter.incrementAndGet(); |
458 final long timestamp = System.currentTimeMillis(); | |
459 final ByteBuffer tBuffer = ByteBuffer.allocate(16); | |
460 tBuffer.putLong(timestamp); | |
461 JungleTree tree = jungle.getTreeByName(_board); | |
462 Either<Error, JungleTreeEditor> either = null; | |
463 do { | |
464 DefaultNodePath path = new DefaultNodePath(); | |
465 path = path.add(Integer.parseInt(_uuid)); | |
466 | |
467 JungleTreeEditor editor = tree.getTreeEditor(); | |
468 NodeEditor e = new NodeEditor() { | |
469 public <T extends EditableNode<T>> Either<Error, T> edit(T _e) { | |
168 | 470 _e = _e.getAttributes().put("author", ByteBuffer.wrap(_author.getBytes())).b(); |
471 _e = _e.getAttributes().put("mes", ByteBuffer.wrap(_message.getBytes())).b(); | |
472 _e = _e.getAttributes().put("key", ByteBuffer.wrap(_editKey.getBytes())).b(); | |
166 | 473 _e = _e.getAttributes().put("timestamp", tBuffer).b(); |
474 return DefaultEither.newB(_e); | |
475 } | |
476 }; | |
477 either = editor.edit(path, e); | |
478 if (either.isA()) { | |
479 throw new IllegalStateException(); | |
480 } | |
481 editor = either.b(); | |
482 either = editor.success(); | |
483 } while (either.isA()); | |
150
eef85b5faa15
Removed renew atribute
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
149
diff
changeset
|
484 renewTime = timestamp; |
38 | 485 } |
486 | |
166 | 487 public Iterable<BoardMessage> getMessages(String _boardName) { |
143
7a7bac51a3cc
Added RequestNumCheckServlet
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
139
diff
changeset
|
488 requestCounter.incrementAndGet(); |
38 | 489 JungleTree tree = jungle.getTreeByName(_boardName); |
490 Node node = tree.getRootNode(); | |
491 Children<Node> chs = node.getChildren(); | |
492 final AtomicInteger counter = new AtomicInteger(0); | |
166 | 493 IterableConverter.Converter<BoardMessage, Node> converter = new IterableConverter.Converter<BoardMessage, Node>() { |
494 public BoardMessage conv(Node _b) { | |
495 String uuid = Integer.toString(counter.get()); | |
168 | 496 String author = new String(_b.getAttributes().get("author") .array()); |
497 String message = new String(_b.getAttributes().get("mes").array()); | |
166 | 498 counter.incrementAndGet(); |
499 return new BoardMessageImpl(author, message, uuid); | |
500 } | |
501 }; | |
502 return new IterableConverter<BoardMessage, Node>(chs, converter); | |
503 } | |
504 | |
505 public Iterable<BoardMessage> getFolder(String _boardName, String _nodeNum) { | |
167 | 506 |
166 | 507 DefaultNodePath path = new DefaultNodePath(); |
508 try { | |
509 for (int count = 0; _nodeNum.substring(count, count + 1) != null; count++) { | |
510 if (!_nodeNum.substring(count, count + 1).equals("/")) | |
168 | 511 path = path.add(Integer.parseInt(_nodeNum.substring(count,count + 1))); |
166 | 512 } |
513 } catch (Exception _e) { | |
514 } | |
515 requestCounter.incrementAndGet(); | |
516 JungleTree tree = jungle.getTreeByName(_boardName); | |
517 Node node = tree.getRootNode(); | |
518 | |
519 DefaultTraverser traverser = new DefaultTraverser(); | |
168 | 520 TraversableNodeWrapper<Node> traversable = new TraversableNodeWrapper<Node>(node); |
166 | 521 DefaultEvaluator evaluator = new DefaultEvaluator(path); |
168 | 522 Either<Error, Traversal<TraversableNodeWrapper<Node>>> ret = traverser.traverse(traversable, evaluator); |
166 | 523 if (ret.isA()) { |
524 Assert.fail(); | |
525 } | |
526 | |
527 Traversal<TraversableNodeWrapper<Node>> traversal = ret.b(); | |
528 TraversableNodeWrapper<Node> target = traversal.destination(); | |
529 Children<Node> chs = target.getWrapped().getChildren(); | |
530 | |
531 final AtomicInteger counter = new AtomicInteger(0); | |
532 IterableConverter.Converter<BoardMessage, Node> converter = new IterableConverter.Converter<BoardMessage, Node>() { | |
38 | 533 public BoardMessage conv(Node _b) { |
534 String uuid = Integer.toString(counter.get()); | |
535 String message = new String(_b.getAttributes().get("mes").array()); | |
536 counter.incrementAndGet(); | |
168 | 537 return new BoardMessageImpl(null, message, uuid); |
38 | 538 } |
539 }; | |
166 | 540 return new IterableConverter<BoardMessage, Node>(chs, converter); |
38 | 541 } |
167 | 542 |
543 | |
544 public getAttributeImp getAttribute(String _boardName, String _nodeNum) { | |
545 | |
546 DefaultNodePath path = new DefaultNodePath(); | |
547 try { | |
548 for (int count = 0; _nodeNum.substring(count, count + 1) != null; count++) { | |
549 if (!_nodeNum.substring(count, count + 1).equals("/")) | |
168 | 550 path = path.add(Integer.parseInt(_nodeNum.substring(count,count + 1))); |
167 | 551 } |
552 } catch (Exception _e) { | |
553 } | |
554 requestCounter.incrementAndGet(); | |
555 JungleTree tree = jungle.getTreeByName(_boardName); | |
556 Node node = tree.getRootNode(); | |
557 | |
558 DefaultTraverser traverser = new DefaultTraverser(); | |
559 TraversableNodeWrapper<Node> traversable = new TraversableNodeWrapper<Node>(node); | |
560 DefaultEvaluator evaluator = new DefaultEvaluator(path); | |
561 Either<Error, Traversal<TraversableNodeWrapper<Node>>> ret = traverser.traverse(traversable, evaluator); | |
562 if (ret.isA()) { | |
563 Assert.fail(); | |
564 } | |
565 | |
566 Traversal<TraversableNodeWrapper<Node>> traversal = ret.b(); | |
567 TraversableNodeWrapper<Node> target = traversal.destination(); | |
568 return new getAttributeImp(target.getWrapped()); | |
569 } | |
166 | 570 |
143
7a7bac51a3cc
Added RequestNumCheckServlet
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
139
diff
changeset
|
571 public int getRequestNum() { |
7a7bac51a3cc
Added RequestNumCheckServlet
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
139
diff
changeset
|
572 return requestCounter.get(); |
7a7bac51a3cc
Added RequestNumCheckServlet
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
139
diff
changeset
|
573 } |
146
29734d7d6521
Added ShowMessageWithTimeStampServlet
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
143
diff
changeset
|
574 |
166 | 575 private static class BoardMessageImpl implements BoardMessage { |
38 | 576 private final String author; |
577 private final String message; | |
578 private final String uuid; | |
166 | 579 |
580 public BoardMessageImpl(String _author, String _message, String _uuid) { | |
38 | 581 author = _author; |
582 message = _message; | |
583 uuid = _uuid; | |
584 } | |
585 | |
166 | 586 public String getAuthor() { |
38 | 587 return author; |
588 } | |
589 | |
166 | 590 public String getMessage() { |
38 | 591 return message; |
592 } | |
593 | |
166 | 594 public String getUUID() { |
38 | 595 return uuid; |
596 } | |
143
7a7bac51a3cc
Added RequestNumCheckServlet
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
139
diff
changeset
|
597 |
38 | 598 } |
124 | 599 |
38 | 600 } |