Mercurial > hg > Database > jungle-network
annotate src/main/java/app/bbs/NetworkJungleBulletinBoard.java @ 168:54d68cda84c4
create edit Attribute & Folder Method
author | tatsuki |
---|---|
date | Tue, 29 Jul 2014 18:51:44 +0900 |
parents | 91f6dd655a01 |
children | 2403b9a4416f |
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; |
166 | 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; | |
165 | 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; | |
165 | 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; |
165 | 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; | |
165 | 34 import junit.framework.Assert; |
38 | 35 |
165 | 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; |
165 | 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; |
165 | 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; |
167 | 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 } |
165 | 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 } |
165 | 58 |
59 public static NetworkBulletinBoard NewPersistentJungle(String _uuid) { | |
167 | 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 } |
165 | 64 |
124 | 65 public void init() { |
165 | 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 } |
165 | 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); |
165 | 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 } |
165 | 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 { |
165 | 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 } |
165 | 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(); |
165 | 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); |
165 | 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(); |
167 | 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(); |
165 | 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 |
165 | 121 public Iterable<String> getBoards() { |
38 | 122 JungleTree tree = jungle.getTreeByName("boards"); |
123 Node node = tree.getRootNode(); | |
124 Children<Node> chs = node.getChildren(); | |
165 | 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 }; | |
165 | 132 |
133 return new IterableConverter<String, Node>(chs, converter); | |
38 | 134 } |
165 | 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 |
167 | 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(); |
165 | 142 if (null == jungle.createNewTree(_name)) { |
38 | 143 throw new IllegalStateException(); |
144 } | |
165 | 145 |
38 | 146 JungleTree tree = jungle.getTreeByName("boards"); |
147 JungleTreeEditor editor = tree.getTreeEditor(); | |
148 DefaultNodePath root = new DefaultNodePath(); | |
165 | 149 Either<Error, JungleTreeEditor> either = editor.addNewChildAt(root, 0); |
150 if (either.isA()) { | |
38 | 151 throw new IllegalStateException(); |
152 } | |
153 editor = either.b(); | |
165 | 154 |
167 | 155 either = editor.putAttribute(root.add(0), "name",ByteBuffer.wrap(_name.getBytes())); |
165 | 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); |
165 | 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(); |
165 | 168 if (either.isA()) { |
38 | 169 throw new IllegalStateException(); |
170 } | |
165 | 171 |
38 | 172 tree = jungle.getTreeByName(_name); |
173 editor = tree.getTreeEditor(); | |
165 | 174 either = editor.addNewChildAt(root, 0); |
175 if (either.isA()) { | |
38 | 176 throw new IllegalStateException(); |
177 } | |
178 editor = either.b(); | |
165 | 179 |
180 NodeEditor e = new NodeEditor() { | |
181 public <T extends EditableNode<T>> Either<Error, T> edit(T _e) { | |
167 | 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); |
165 | 187 _e = _e.getAttributes().put("timestamp", tBuffer2).b(); |
38 | 188 return DefaultEither.newB(_e); |
189 } | |
190 }; | |
165 | 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 | |
167 | 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); |
165 | 202 if (tree == null) { |
38 | 203 throw new IllegalStateException(); |
204 } | |
166 | 205 |
165 | 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("/")) | |
167 | 210 path = path.add(Integer.parseInt(_nodeNum.substring(count, count + 1))); |
165 | 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); |
167 | 219 |
165 | 220 do { |
38 | 221 Node node = tree.getRootNode(); |
165 | 222 DefaultTraverser traverser = new DefaultTraverser(); |
167 | 223 TraversableNodeWrapper<Node> traversable = new TraversableNodeWrapper<Node>(node); |
165 | 224 DefaultEvaluator evaluator = new DefaultEvaluator(path); |
167 | 225 Either<Error, Traversal<TraversableNodeWrapper<Node>>> ret = traverser.traverse(traversable, evaluator); |
165 | 226 if (ret.isA()) { |
227 Assert.fail(); | |
228 } | |
229 | |
230 Traversal<TraversableNodeWrapper<Node>> traversal = ret.b(); | |
231 TraversableNodeWrapper<Node> target = traversal.destination(); | |
166 | 232 int size = target.getWrapped().getChildren().size(); |
65 | 233 JungleTreeEditor editor = tree.getTreeEditor(); |
165 | 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 |
165 | 240 NodeEditor e = new NodeEditor() { |
241 public <T extends EditableNode<T>> Either<Error, T> edit(T _e) { | |
167 | 242 _e = _e.getAttributes().put("mes", ByteBuffer.wrap(_message.getBytes())).b(); |
165 | 243 _e = _e.getAttributes().put("timestamp", tBuffer).b(); |
38 | 244 return DefaultEither.newB(_e); |
245 } | |
246 }; | |
247 path = path.add(size); | |
165 | 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(); |
165 | 254 } while (either.isA()); |
93 | 255 |
38 | 256 } |
257 | |
167 | 258 public void createBoardMessage(final String _board, final String _author, final String _message, final String _editKey) { |
165 | 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 { | |
167 | 270 |
165 | 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) { | |
167 | 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(); | |
165 | 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 _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; |
167 | 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(); |
168 | 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(); |
165 | 320 NodeEditor e = new NodeEditor() { |
321 public <T extends EditableNode<T>> Either<Error, T> edit(T _e) { | |
167 | 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(); | |
165 | 325 _e = _e.getAttributes().put("timestamp", tBuffer).b(); |
326 return DefaultEither.newB(_e); | |
38 | 327 } |
328 }; | |
165 | 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(); |
165 | 335 } while (either.isA()); |
336 renewTime = timestamp; | |
337 } | |
166 | 338 |
167 | 339 public void createAttribute(String _board, String _uuid, final String _author, final String _message, final String _editKey) { |
165 | 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("/")) | |
167 | 351 path = path.add(Integer.parseInt(_uuid.substring(count, count + 1))); |
165 | 352 } |
353 } catch (Exception _e) { | |
354 } | |
167 | 355 |
165 | 356 JungleTreeEditor editor = tree.getTreeEditor(); |
357 NodeEditor e = new NodeEditor() { | |
358 public <T extends EditableNode<T>> Either<Error, T> edit(T _e) { | |
166 | 359 // EnableNodeWrapper<T> node = _e.getWrap(); |
360 String str = "0"; | |
361 int count = 0; | |
167 | 362 for (; _e.getAttributes().get("mes" + String.valueOf(count)) != null; count++) { |
166 | 363 } |
168 | 364 str = String.valueOf(count); |
166 | 365 _e = _e.getAttributes().put("mes" + str, ByteBuffer.wrap(_message.getBytes())).b(); |
366 _e = _e.getAttributes().put("timestamp" + str, tBuffer).b(); | |
165 | 367 return DefaultEither.newB(_e); |
368 } | |
369 }; | |
370 either = editor.edit(path, e); | |
371 if (either.isA()) { | |
372 throw new IllegalStateException(); | |
373 } | |
374 editor = either.b(); | |
375 either = editor.success(); | |
376 } while (either.isA()); | |
377 } | |
166 | 378 |
168 | 379 public void editAttribute(String boardName, String _path, String id, String _message) { |
380 requestCounter.incrementAndGet(); | |
381 final long timestamp = System.currentTimeMillis(); | |
382 final ByteBuffer tBuffer = ByteBuffer.allocate(16); | |
383 tBuffer.putLong(timestamp); | |
384 JungleTree tree = jungle.getTreeByName(boardName); | |
385 Either<Error, JungleTreeEditor> either = null; | |
386 DefaultNodePath path = new DefaultNodePath(); | |
387 do { | |
388 try { | |
389 for (int count = 0; _path.substring(count, count + 1) != null; count++) { | |
390 if (!_path.substring(count, count + 1).equals("/")) | |
391 path = path.add(Integer.parseInt(_path.substring(count, count + 1))); | |
392 } | |
393 } catch (Exception _e) { | |
394 } | |
395 | |
396 JungleTreeEditor editor = tree.getTreeEditor(); | |
397 NodeEditor e = new NodeEditor() { | |
398 public <T extends EditableNode<T>> Either<Error, T> edit(T _e) { | |
399 // EnableNodeWrapper<T> node = _e.getWrap(); | |
400 _e = _e.getAttributes().put("mes" + id, ByteBuffer.wrap(_message.getBytes())).b(); | |
401 _e = _e.getAttributes().put("timestamp" + id, tBuffer).b(); | |
402 return DefaultEither.newB(_e); | |
403 } | |
404 }; | |
405 either = editor.edit(path, e); | |
406 if (either.isA()) { | |
407 throw new IllegalStateException(); | |
408 } | |
409 editor = either.b(); | |
410 either = editor.success(); | |
411 } while (either.isA()); | |
412 } | |
413 | |
414 | |
167 | 415 public void editMatrixMessage(String _board, String _uuid, final String _author, final String _message, final String _editKey) { |
165 | 416 requestCounter.incrementAndGet(); |
417 final long timestamp = System.currentTimeMillis(); | |
418 final ByteBuffer tBuffer = ByteBuffer.allocate(16); | |
419 tBuffer.putLong(timestamp); | |
420 JungleTree tree = jungle.getTreeByName(_board); | |
421 Either<Error, JungleTreeEditor> either = null; | |
422 do { | |
423 DefaultNodePath path = new DefaultNodePath(); | |
424 path = path.add(Integer.parseInt(_uuid)); | |
425 | |
426 JungleTreeEditor editor = tree.getTreeEditor(); | |
427 NodeEditor e = new NodeEditor() { | |
428 public <T extends EditableNode<T>> Either<Error, T> edit(T _e) { | |
167 | 429 _e = _e.getAttributes().put("author", ByteBuffer.wrap(_author.getBytes())).b(); |
430 _e = _e.getAttributes().put("mes", ByteBuffer.wrap(_message.getBytes())).b(); | |
431 _e = _e.getAttributes().put("key", ByteBuffer.wrap(_editKey.getBytes())).b(); | |
165 | 432 _e = _e.getAttributes().put("timestamp", tBuffer).b(); |
433 return DefaultEither.newB(_e); | |
434 } | |
435 }; | |
436 either = editor.edit(path, e); | |
437 if (either.isA()) { | |
438 throw new IllegalStateException(); | |
439 } | |
440 editor = either.b(); | |
441 either = editor.success(); | |
442 } while (either.isA()); | |
150
eef85b5faa15
Removed renew atribute
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
149
diff
changeset
|
443 renewTime = timestamp; |
38 | 444 } |
445 | |
165 | 446 public Iterable<BoardMessage> getMessages(String _boardName) { |
143
7a7bac51a3cc
Added RequestNumCheckServlet
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
139
diff
changeset
|
447 requestCounter.incrementAndGet(); |
38 | 448 JungleTree tree = jungle.getTreeByName(_boardName); |
449 Node node = tree.getRootNode(); | |
450 Children<Node> chs = node.getChildren(); | |
451 final AtomicInteger counter = new AtomicInteger(0); | |
165 | 452 IterableConverter.Converter<BoardMessage, Node> converter = new IterableConverter.Converter<BoardMessage, Node>() { |
453 public BoardMessage conv(Node _b) { | |
454 String uuid = Integer.toString(counter.get()); | |
167 | 455 String author = new String(_b.getAttributes().get("author") .array()); |
456 String message = new String(_b.getAttributes().get("mes").array()); | |
165 | 457 counter.incrementAndGet(); |
458 return new BoardMessageImpl(author, message, uuid); | |
459 } | |
460 }; | |
461 return new IterableConverter<BoardMessage, Node>(chs, converter); | |
462 } | |
463 | |
464 public Iterable<BoardMessage> getFolder(String _boardName, String _nodeNum) { | |
166 | 465 |
165 | 466 DefaultNodePath path = new DefaultNodePath(); |
467 try { | |
468 for (int count = 0; _nodeNum.substring(count, count + 1) != null; count++) { | |
469 if (!_nodeNum.substring(count, count + 1).equals("/")) | |
167 | 470 path = path.add(Integer.parseInt(_nodeNum.substring(count,count + 1))); |
165 | 471 } |
472 } catch (Exception _e) { | |
473 } | |
474 requestCounter.incrementAndGet(); | |
475 JungleTree tree = jungle.getTreeByName(_boardName); | |
476 Node node = tree.getRootNode(); | |
477 | |
478 DefaultTraverser traverser = new DefaultTraverser(); | |
167 | 479 TraversableNodeWrapper<Node> traversable = new TraversableNodeWrapper<Node>(node); |
165 | 480 DefaultEvaluator evaluator = new DefaultEvaluator(path); |
167 | 481 Either<Error, Traversal<TraversableNodeWrapper<Node>>> ret = traverser.traverse(traversable, evaluator); |
165 | 482 if (ret.isA()) { |
483 Assert.fail(); | |
484 } | |
485 | |
486 Traversal<TraversableNodeWrapper<Node>> traversal = ret.b(); | |
487 TraversableNodeWrapper<Node> target = traversal.destination(); | |
488 Children<Node> chs = target.getWrapped().getChildren(); | |
489 | |
490 final AtomicInteger counter = new AtomicInteger(0); | |
491 IterableConverter.Converter<BoardMessage, Node> converter = new IterableConverter.Converter<BoardMessage, Node>() { | |
38 | 492 public BoardMessage conv(Node _b) { |
493 String uuid = Integer.toString(counter.get()); | |
494 String message = new String(_b.getAttributes().get("mes").array()); | |
495 counter.incrementAndGet(); | |
167 | 496 return new BoardMessageImpl(null, message, uuid); |
38 | 497 } |
498 }; | |
165 | 499 return new IterableConverter<BoardMessage, Node>(chs, converter); |
38 | 500 } |
166 | 501 |
502 | |
503 public getAttributeImp getAttribute(String _boardName, String _nodeNum) { | |
504 | |
505 DefaultNodePath path = new DefaultNodePath(); | |
506 try { | |
507 for (int count = 0; _nodeNum.substring(count, count + 1) != null; count++) { | |
508 if (!_nodeNum.substring(count, count + 1).equals("/")) | |
167 | 509 path = path.add(Integer.parseInt(_nodeNum.substring(count,count + 1))); |
166 | 510 } |
511 } catch (Exception _e) { | |
512 } | |
513 requestCounter.incrementAndGet(); | |
514 JungleTree tree = jungle.getTreeByName(_boardName); | |
515 Node node = tree.getRootNode(); | |
516 | |
517 DefaultTraverser traverser = new DefaultTraverser(); | |
518 TraversableNodeWrapper<Node> traversable = new TraversableNodeWrapper<Node>(node); | |
519 DefaultEvaluator evaluator = new DefaultEvaluator(path); | |
520 Either<Error, Traversal<TraversableNodeWrapper<Node>>> ret = traverser.traverse(traversable, evaluator); | |
521 if (ret.isA()) { | |
522 Assert.fail(); | |
523 } | |
524 | |
525 Traversal<TraversableNodeWrapper<Node>> traversal = ret.b(); | |
526 TraversableNodeWrapper<Node> target = traversal.destination(); | |
527 return new getAttributeImp(target.getWrapped()); | |
528 } | |
165 | 529 |
143
7a7bac51a3cc
Added RequestNumCheckServlet
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
139
diff
changeset
|
530 public int getRequestNum() { |
7a7bac51a3cc
Added RequestNumCheckServlet
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
139
diff
changeset
|
531 return requestCounter.get(); |
7a7bac51a3cc
Added RequestNumCheckServlet
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
139
diff
changeset
|
532 } |
146
29734d7d6521
Added ShowMessageWithTimeStampServlet
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
143
diff
changeset
|
533 |
165 | 534 private static class BoardMessageImpl implements BoardMessage { |
38 | 535 private final String author; |
536 private final String message; | |
537 private final String uuid; | |
165 | 538 |
539 public BoardMessageImpl(String _author, String _message, String _uuid) { | |
38 | 540 author = _author; |
541 message = _message; | |
542 uuid = _uuid; | |
543 } | |
544 | |
165 | 545 public String getAuthor() { |
38 | 546 return author; |
547 } | |
548 | |
165 | 549 public String getMessage() { |
38 | 550 return message; |
551 } | |
552 | |
165 | 553 public String getUUID() { |
38 | 554 return uuid; |
555 } | |
143
7a7bac51a3cc
Added RequestNumCheckServlet
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
139
diff
changeset
|
556 |
38 | 557 } |
124 | 558 |
168 | 559 |
560 | |
38 | 561 } |