|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
SymbolSerializer.java | 50% | 87% | 60% | 76.5% |
|
1 | /* | |
2 | * Licensed to the Apache Software Foundation (ASF) under one or more | |
3 | * contributor license agreements. See the NOTICE file distributed with | |
4 | * this work for additional information regarding copyright ownership. | |
5 | * The ASF licenses this file to You under the Apache License, Version 2.0 | |
6 | * (the "License"); you may not use this file except in compliance with | |
7 | * the License. You may obtain a copy of the License at | |
8 | * | |
9 | * http://www.apache.org/licenses/LICENSE-2.0 | |
10 | * | |
11 | * Unless required by applicable law or agreed to in writing, software | |
12 | * distributed under the License is distributed on an "AS IS" BASIS, | |
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
14 | * See the License for the specific language governing permissions and | |
15 | * limitations under the License. | |
16 | * | |
17 | * $Id: SymbolSerializer.java 712565 2008-11-09 21:52:26Z vgritsenko $ | |
18 | */ | |
19 | ||
20 | package org.apache.xindice.util; | |
21 | ||
22 | import org.apache.commons.logging.Log; | |
23 | import org.apache.commons.logging.LogFactory; | |
24 | import org.apache.xindice.xml.SymbolTable; | |
25 | import org.apache.xindice.xml.SymbolTableSymbols; | |
26 | import org.apache.xindice.xml.dom.CompressedDocument; | |
27 | import org.apache.xindice.xml.dom.DOMCompressor; | |
28 | import org.apache.xindice.xml.dom.DocumentImpl; | |
29 | ||
30 | import org.w3c.dom.Document; | |
31 | import org.w3c.dom.Element; | |
32 | ||
33 | import java.util.HashMap; | |
34 | import java.util.Map; | |
35 | ||
36 | /** | |
37 | * SymbolSerializer is a utility class for managing SymbolTables in | |
38 | * the server context of Wire Compression. | |
39 | * | |
40 | * @version $Revision: 712565 $, $Date: 2008-11-09 21:52:26 +0000 (Sun, 09 Nov 2008) $ | |
41 | */ | |
42 | public final class SymbolSerializer { | |
43 | ||
44 | private static final Log log = LogFactory.getLog(SymbolSerializer.class); | |
45 | ||
46 | /** | |
47 | * The collection's SymbolTable | |
48 | */ | |
49 | private final SymbolTable syms; | |
50 | ||
51 | /** | |
52 | * Last time we caught a SymbolTable modification | |
53 | */ | |
54 | private long lastMod; | |
55 | ||
56 | /** | |
57 | * Stores a byte representation of the SymbolTable | |
58 | */ | |
59 | private byte[] symBytes; | |
60 | ||
61 | ||
62 | 169 | public SymbolSerializer(SymbolTable syms) { |
63 | 169 | this.syms = syms; |
64 | } | |
65 | ||
66 | /** | |
67 | * getSymbols returns the Symbol Table being managed by this | |
68 | * Serializer. | |
69 | * | |
70 | * @return The Symbol Table | |
71 | */ | |
72 | 0 | public SymbolTable getSymbols() { |
73 | 0 | return syms; |
74 | } | |
75 | ||
76 | /** | |
77 | * getLastModified returns a time stamp of the last server-side | |
78 | * modified of the Symbol Table. This is used to determine | |
79 | * whether or not to regenerate the byte stream, and whether | |
80 | * the client will need a new copy of the Symbol Table. | |
81 | * | |
82 | * @return Last modified stamp | |
83 | */ | |
84 | 0 | public long getLastModified() { |
85 | 0 | return syms.getLastModified(); |
86 | } | |
87 | ||
88 | /** | |
89 | * getSymBuffer returns a new Map that optionally includes a | |
90 | * reference to the SymbolTable's byte array image for wire | |
91 | * transmission. | |
92 | * | |
93 | * @return A new Map | |
94 | */ | |
95 | 169 | private Map getSymBuffer(long remoteLastMod) { |
96 | 169 | long lm = syms.getLastModified(); |
97 | 169 | if (lm != lastMod) { |
98 | 169 | DocumentImpl doc = new DocumentImpl(); |
99 | 169 | doc.setSymbols(SymbolTableSymbols.getInstance()); |
100 | ||
101 | 169 | synchronized (syms) { |
102 | 169 | Element element = syms.streamToXML(doc); |
103 | 169 | doc.appendChild(element); |
104 | ||
105 | 169 | symBytes = DOMCompressor.compress(doc, SymbolTableSymbols.getInstance()); |
106 | 169 | lastMod = lm; |
107 | } | |
108 | } | |
109 | ||
110 | 169 | Map result = new HashMap(5); |
111 | 169 | if (remoteLastMod != lastMod) { |
112 | 169 | if (log.isDebugEnabled()) { |
113 | 0 | log.info("Sending symbols. Client ts=" + remoteLastMod + ", Server ts=" + lastMod); |
114 | } | |
115 | 169 | result.put("timestamp", Long.toString(lastMod)); |
116 | 169 | result.put("symbols", symBytes); |
117 | } | |
118 | ||
119 | 169 | return result; |
120 | } | |
121 | ||
122 | /** | |
123 | * Converts a DOM Document into a Map that, depending on the | |
124 | * time stamp, possibly includes a current image of the managed | |
125 | * Collection's Symbol Table. | |
126 | * | |
127 | * @param doc The Document to Convert | |
128 | * @param remoteLastMod The client's last modified time stamp | |
129 | * @return The Map | |
130 | */ | |
131 | 169 | public Map serialize(Document doc, long remoteLastMod) { |
132 | 169 | Map result = getSymBuffer(remoteLastMod); |
133 | 169 | byte[] docBytes = ((CompressedDocument) doc).getDataBytes(); |
134 | 169 | result.put("document", docBytes); |
135 | ||
136 | 169 | return result; |
137 | } | |
138 | } |
|