| 1 |
|
package org.trails.io; |
| 2 |
|
|
| 3 |
|
import org.apache.commons.logging.Log; |
| 4 |
|
import org.apache.commons.logging.LogFactory; |
| 5 |
|
import org.apache.hivemind.ApplicationRuntimeException; |
| 6 |
|
import org.apache.tapestry.services.DataSqueezer; |
| 7 |
|
import org.apache.tapestry.util.io.SqueezeAdaptor; |
| 8 |
|
import org.trails.util.Utils; |
| 9 |
|
import org.trails.descriptor.DescriptorService; |
| 10 |
|
import org.trails.descriptor.IClassDescriptor; |
| 11 |
|
import org.trails.engine.encoders.abbreviator.EntityNameAbbreviator; |
| 12 |
|
import org.trails.persistence.PersistenceService; |
| 13 |
|
import org.trails.builder.BuilderDirector; |
| 14 |
|
import org.trails.exception.TrailsRuntimeException; |
| 15 |
|
|
| 16 |
|
import java.io.Serializable; |
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
|
| 21 |
36 |
public class EntitySqueezerStrategy implements SqueezeFilter, SqueezeAdaptor |
| 22 |
|
{ |
| 23 |
4 |
private static final Log LOG = LogFactory.getLog(EntitySqueezerStrategy.class); |
| 24 |
|
|
| 25 |
|
String delimiter; |
| 26 |
|
String prefix; |
| 27 |
|
|
| 28 |
|
private DescriptorService descriptorService; |
| 29 |
|
|
| 30 |
|
private PersistenceService persistenceService; |
| 31 |
|
|
| 32 |
|
private EntityNameAbbreviator entityNameAbbreviator; |
| 33 |
16 |
private boolean shouldAbbreviate = false; |
| 34 |
|
|
| 35 |
|
private BuilderDirector builderDirector; |
| 36 |
|
|
| 37 |
|
public String getPrefix() |
| 38 |
|
{ |
| 39 |
0 |
return prefix; |
| 40 |
|
} |
| 41 |
|
|
| 42 |
|
public Class getDataClass() |
| 43 |
|
{ |
| 44 |
0 |
return Squeezable.class; |
| 45 |
|
} |
| 46 |
|
|
| 47 |
|
public String squeeze(DataSqueezer dataSqueezer, Object o) |
| 48 |
|
{ |
| 49 |
0 |
return squeeze(o, dataSqueezer); |
| 50 |
|
} |
| 51 |
|
|
| 52 |
|
public Object unsqueeze(DataSqueezer dataSqueezer, String s) |
| 53 |
|
{ |
| 54 |
0 |
return unsqueeze(s, dataSqueezer); |
| 55 |
|
} |
| 56 |
|
|
| 57 |
|
public String squeeze(Object data, DataSqueezer next) |
| 58 |
|
{ |
| 59 |
|
|
| 60 |
4 |
if (data != null) |
| 61 |
|
{ |
| 62 |
4 |
IClassDescriptor classDescriptor = descriptorService.getClassDescriptor(data.getClass()); |
| 63 |
4 |
if (classDescriptor != null) |
| 64 |
|
{ |
| 65 |
4 |
Serializable id = persistenceService.getIdentifier(data, classDescriptor); |
| 66 |
|
|
| 67 |
4 |
if (LOG.isDebugEnabled()) |
| 68 |
|
{ |
| 69 |
0 |
LOG.debug("squeezing entity: " + data.toString()); |
| 70 |
|
} |
| 71 |
|
|
| 72 |
4 |
if (id == null) |
| 73 |
|
{ |
| 74 |
0 |
return prefix + entityNameAbbreviator.abbreviate(classDescriptor.getType()); |
| 75 |
|
} |
| 76 |
|
|
| 77 |
|
|
| 78 |
4 |
return prefix + abbreviate(classDescriptor.getType()) + delimiter + next.squeeze(id); |
| 79 |
|
} |
| 80 |
|
} |
| 81 |
0 |
return next.squeeze(data); |
| 82 |
|
} |
| 83 |
|
|
| 84 |
|
public String[] squeeze(Object[] objects, DataSqueezer next) |
| 85 |
|
{ |
| 86 |
0 |
final String[] squeezed = new String[objects.length]; |
| 87 |
0 |
for (int i = 0; i < squeezed.length; i++) |
| 88 |
|
{ |
| 89 |
0 |
squeezed[i] = squeeze(objects[i], next); |
| 90 |
|
} |
| 91 |
0 |
return squeezed; |
| 92 |
|
} |
| 93 |
|
|
| 94 |
|
public Object unsqueeze(String string, DataSqueezer next) |
| 95 |
|
{ |
| 96 |
4 |
if (string.startsWith(prefix)) |
| 97 |
|
{ |
| 98 |
4 |
final String squeezed = string.substring(prefix.length()); |
| 99 |
4 |
final int delimiterNdx = squeezed.indexOf(delimiter); |
| 100 |
|
|
| 101 |
|
final String entityName; |
| 102 |
|
final Serializable id; |
| 103 |
|
|
| 104 |
4 |
if (delimiterNdx > 0) |
| 105 |
|
{ |
| 106 |
4 |
entityName = squeezed.substring(0, delimiterNdx); |
| 107 |
4 |
id = (Serializable) next.unsqueeze(squeezed.substring(delimiterNdx + delimiter.length())); |
| 108 |
|
} else |
| 109 |
|
{ |
| 110 |
0 |
entityName = squeezed; |
| 111 |
0 |
id = null; |
| 112 |
|
} |
| 113 |
|
|
| 114 |
4 |
final Class<?> clazz = (Class<?>) unabbreviate(entityName); |
| 115 |
|
|
| 116 |
4 |
if (LOG.isDebugEnabled()) |
| 117 |
|
{ |
| 118 |
0 |
LOG.debug("unsqueezing entity: " + clazz.getName() + " : " + id); |
| 119 |
|
} |
| 120 |
|
|
| 121 |
4 |
if (id != null) |
| 122 |
|
{ |
| 123 |
4 |
return persistenceService.loadInstance(clazz, id); |
| 124 |
|
} else |
| 125 |
|
{ |
| 126 |
|
try |
| 127 |
|
{ |
| 128 |
0 |
return builderDirector.createNewInstance(clazz); |
| 129 |
0 |
} catch (TrailsRuntimeException e) |
| 130 |
|
{ |
| 131 |
0 |
throw new ApplicationRuntimeException("decode-failure: unable to unsqueeze entity", e); |
| 132 |
|
} |
| 133 |
|
} |
| 134 |
|
} else |
| 135 |
|
{ |
| 136 |
0 |
return next.unsqueeze(string); |
| 137 |
|
} |
| 138 |
|
} |
| 139 |
|
|
| 140 |
|
public Object[] unsqueeze(String[] strings, DataSqueezer next) |
| 141 |
|
{ |
| 142 |
0 |
final Object[] unsqueezed = new Object[strings.length]; |
| 143 |
0 |
for (int i = 0; i < unsqueezed.length; i++) |
| 144 |
|
{ |
| 145 |
0 |
unsqueezed[i] = unsqueeze(strings[i], next); |
| 146 |
|
} |
| 147 |
0 |
return unsqueezed; |
| 148 |
|
} |
| 149 |
|
|
| 150 |
|
public void setDelimiter(String delimiter) |
| 151 |
|
{ |
| 152 |
8 |
this.delimiter = delimiter; |
| 153 |
8 |
} |
| 154 |
|
|
| 155 |
|
public void setPrefix(String prefix) |
| 156 |
|
{ |
| 157 |
8 |
this.prefix = prefix; |
| 158 |
8 |
} |
| 159 |
|
|
| 160 |
|
public void setDescriptorService(DescriptorService descriptorService) |
| 161 |
|
{ |
| 162 |
8 |
this.descriptorService = descriptorService; |
| 163 |
8 |
} |
| 164 |
|
|
| 165 |
|
public void setPersistenceService(PersistenceService persistenceService) |
| 166 |
|
{ |
| 167 |
8 |
this.persistenceService = persistenceService; |
| 168 |
8 |
} |
| 169 |
|
|
| 170 |
|
public void setEntityNameAbbreviator(EntityNameAbbreviator entityNameAbbreviator) |
| 171 |
|
{ |
| 172 |
0 |
this.entityNameAbbreviator = entityNameAbbreviator; |
| 173 |
0 |
shouldAbbreviate = entityNameAbbreviator != null; |
| 174 |
0 |
} |
| 175 |
|
|
| 176 |
|
public void setBuilderDirector(BuilderDirector builderDirector) |
| 177 |
|
{ |
| 178 |
0 |
this.builderDirector = builderDirector; |
| 179 |
0 |
} |
| 180 |
|
|
| 181 |
|
private String abbreviate(Class clazz) |
| 182 |
|
{ |
| 183 |
4 |
return shouldAbbreviate ? entityNameAbbreviator.abbreviate(clazz) : clazz.getName(); |
| 184 |
|
} |
| 185 |
|
|
| 186 |
|
private Class unabbreviate(String abbreviation) |
| 187 |
|
{ |
| 188 |
4 |
return shouldAbbreviate ? entityNameAbbreviator.unabbreviate(abbreviation) : Utils.classForName(abbreviation); |
| 189 |
|
} |
| 190 |
|
} |