@Override public Writable getHeuristicNext(Map<String, Writable> writables) { if (getNextWritables().isEmpty()) { return null; } HashSet<Writable> temp = new HashSet<Writable>(); for (String s : getNextWritables()) temp.add(writables.get(s)); TreeSet<Writable> ordered = new TreeSet<Writable>( new Comparator<Writable>() { public int compare(Writable one, Writable two) { return Integer.compare(one.getUseCount(), two.getUseCount()); } }); ordered.addAll(temp); ArrayList<Writable> heuristicPick = new ArrayList<Writable>(); int count = 0; Word last = null; for (Writable w : ordered) { if (last == null || !(last.getUseCount() == w.getUseCount())) count++; for (int k = 0; k < count; k++) { heuristicPick.add(w); } } return heuristicPick.get((int) (Math.random() * heuristicPick.size())); }
private boolean renderTemplate( Object target, GroovyObject controller, GrailsWebRequest webRequest, Map argMap, Writer out) { boolean renderView; String templateName = argMap.get(ARGUMENT_TEMPLATE).toString(); String contextPath = getContextPath(webRequest, argMap); String var = (String) argMap.get(ARGUMENT_VAR); // get the template uri String templateUri = GroovyPageUtils.getTemplateURI(controller, templateName); // retrieve gsp engine GroovyPagesTemplateEngine engine = (GroovyPagesTemplateEngine) webRequest.getApplicationContext().getBean(GroovyPagesTemplateEngine.BEAN_ID); try { Resource r = engine.getResourceForUri(contextPath + templateUri); if (!r.exists()) { r = engine.getResourceForUri(contextPath + "/grails-app/views/" + templateUri); } Template t = engine.createTemplate(r); // templateUri); if (t == null) { throw new ControllerExecutionException( "Unable to load template for uri [" + templateUri + "]. Template not found."); } Map binding = new HashMap(); if (argMap.containsKey(ARGUMENT_BEAN)) { Object bean = argMap.get(ARGUMENT_BEAN); if (argMap.containsKey(ARGUMENT_MODEL)) { Object modelObject = argMap.get(ARGUMENT_MODEL); if (modelObject instanceof Map) binding.putAll((Map) modelObject); } renderTemplateForBean(t, binding, bean, var, out); } else if (argMap.containsKey(ARGUMENT_COLLECTION)) { Object colObject = argMap.get(ARGUMENT_COLLECTION); if (argMap.containsKey(ARGUMENT_MODEL)) { Object modelObject = argMap.get(ARGUMENT_MODEL); if (modelObject instanceof Map) binding.putAll((Map) modelObject); } renderTemplateForCollection(t, binding, colObject, var, out); } else if (argMap.containsKey(ARGUMENT_MODEL)) { Object modelObject = argMap.get(ARGUMENT_MODEL); renderTemplateForModel(t, modelObject, target, out); } else { Writable w = t.make(new BeanMap(target)); w.writeTo(out); } renderView = false; } catch (GroovyRuntimeException gre) { throw new ControllerExecutionException( "Error rendering template [" + templateName + "]: " + gre.getMessage(), gre); } catch (IOException ioex) { throw new ControllerExecutionException( "I/O error executing render method for arguments [" + argMap + "]: " + ioex.getMessage(), ioex); } return renderView; }
@Override @SuppressWarnings("unchecked") public Writable put(Writable key, Writable value) { addToMap(key.getClass()); addToMap(value.getClass()); return instance.put(key, value); }
@SuppressWarnings("unchecked") @Override public void readFields(DataInput in) throws IOException { super.readFields(in); // First clear the map. Otherwise we will just accumulate // entries every time this method is called. this.instance.clear(); // Read the number of entries in the map int entries = in.readInt(); // Then read each key/value pair for (int i = 0; i < entries; i++) { Writable key = (Writable) ReflectionUtils.newInstance(getClass(in.readByte()), getConf()); key.readFields(in); Writable value = (Writable) ReflectionUtils.newInstance(getClass(in.readByte()), getConf()); value.readFields(in); instance.put(key, value); } }
private void renderTemplateForBean( Template template, Map binding, Object bean, String varName, Writer out) throws IOException { if (StringUtils.isBlank(varName)) { binding.put(DEFAULT_ARGUMENT, bean); } else binding.put(varName, bean); Writable w = template.make(binding); w.writeTo(out); }
public void readFields(DataInput in) throws IOException { values = new Writable[in.readInt()]; // construct values for (int i = 0; i < values.length; i++) { Writable value = WritableFactories.newInstance(valueClass); value.readFields(in); // read a value values[i] = value; // store it in values } }
private void renderTemplateForModel( Template template, Object modelObject, Object target, Writer out) throws IOException { if (modelObject instanceof Map) { Writable w = template.make((Map) modelObject); w.writeTo(out); } else { Writable w = template.make(new BeanMap(target)); w.writeTo(out); } }
/** Convert writables to a byte array */ public static byte[] toByteArray(Writable... writables) { final DataOutputBuffer out = new DataOutputBuffer(); try { for (Writable w : writables) { w.write(out); } out.close(); } catch (IOException e) { throw new RuntimeException("Fail to convert writables to a byte array", e); } return out.getData(); }
/** * Make a copy of a writable object using serialization to a buffer. * * @param orig The object to copy * @return The copied object */ public static Writable clone(Writable orig, JobConf conf) { try { Writable newInst = (Writable) conf.newInstance(orig.getClass()); CopyInCopyOutBuffer buffer = (CopyInCopyOutBuffer) cloneBuffers.get(); buffer.outBuffer.reset(); orig.write(buffer.outBuffer); buffer.moveData(); newInst.readFields(buffer.inBuffer); return newInst; } catch (IOException e) { throw new RuntimeException("Error writing/reading clone buffer", e); } }
/** Utility method for testing writables. */ public static Writable testWritable(Writable before, Configuration conf) throws Exception { DataOutputBuffer dob = new DataOutputBuffer(); before.write(dob); DataInputBuffer dib = new DataInputBuffer(); dib.reset(dob.getData(), dob.getLength()); Writable after = (Writable) ReflectionUtils.newInstance(before.getClass(), conf); after.readFields(dib); assertEquals(before, after); return after; }
/** Write an operation to the edit log */ void logEdit(byte op, Writable w1, Writable w2) { synchronized (editlog) { try { editlog.write(op); if (w1 != null) { w1.write(editlog); } if (w2 != null) { w2.write(editlog); } } catch (IOException ie) { } } }
private boolean renderMarkup(Closure closure, HttpServletResponse response) { boolean renderView; StreamingMarkupBuilder b = new StreamingMarkupBuilder(); Writable markup = (Writable) b.bind(closure); try { markup.writeTo(response.getWriter()); } catch (IOException e) { throw new ControllerExecutionException( "I/O error executing render method for arguments [" + closure + "]: " + e.getMessage(), e); } renderView = false; return renderView; }
private void renderTemplateForCollection( Template template, Map binding, Object colObject, String var, Writer out) throws IOException { if (colObject instanceof Collection) { Collection c = (Collection) colObject; for (Iterator i = c.iterator(); i.hasNext(); ) { Object o = i.next(); if (StringUtils.isBlank(var)) binding.put(DEFAULT_ARGUMENT, o); else binding.put(var, o); Writable w = template.make(binding); w.writeTo(out); } } else { if (StringUtils.isBlank(var)) binding.put(DEFAULT_ARGUMENT, colObject); else binding.put(var, colObject); Writable w = template.make(binding); w.writeTo(out); } }
/** {@inheritDoc} */ @Override public void writeExternal(ObjectOutput out) throws IOException { out.writeInt(id); boolean writable = innerSplit instanceof Writable; out.writeUTF(writable ? innerSplit.getClass().getName() : null); if (writable) ((Writable) innerSplit).write(out); else out.writeObject(innerSplit); }
/** Write a {@link Writable}, {@link String}, primitive type, or an array of the preceding. */ public static void writeObject( DataOutput out, Object instance, Class declaredClass, Configuration conf) throws IOException { if (instance == null) { // null instance = new NullInstance(declaredClass, conf); declaredClass = Writable.class; } UTF8.writeString(out, declaredClass.getName()); // always write declared if (declaredClass.isArray()) { // array int length = Array.getLength(instance); out.writeInt(length); for (int i = 0; i < length; i++) { writeObject(out, Array.get(instance, i), declaredClass.getComponentType(), conf); } } else if (declaredClass == String.class) { // String UTF8.writeString(out, (String) instance); } else if (declaredClass.isPrimitive()) { // primitive type if (declaredClass == Boolean.TYPE) { // boolean out.writeBoolean(((Boolean) instance).booleanValue()); } else if (declaredClass == Character.TYPE) { // char out.writeChar(((Character) instance).charValue()); } else if (declaredClass == Byte.TYPE) { // byte out.writeByte(((Byte) instance).byteValue()); } else if (declaredClass == Short.TYPE) { // short out.writeShort(((Short) instance).shortValue()); } else if (declaredClass == Integer.TYPE) { // int out.writeInt(((Integer) instance).intValue()); } else if (declaredClass == Long.TYPE) { // long out.writeLong(((Long) instance).longValue()); } else if (declaredClass == Float.TYPE) { // float out.writeFloat(((Float) instance).floatValue()); } else if (declaredClass == Double.TYPE) { // double out.writeDouble(((Double) instance).doubleValue()); } else if (declaredClass == Void.TYPE) { // void } else { throw new IllegalArgumentException("Not a primitive: " + declaredClass); } } else if (declaredClass.isEnum()) { // enum UTF8.writeString(out, ((Enum) instance).name()); } else if (Writable.class.isAssignableFrom(declaredClass)) { // Writable UTF8.writeString(out, instance.getClass().getName()); ((Writable) instance).write(out); } else { throw new IOException("Can't write: " + instance + " as " + declaredClass); } }
@Override public void readFields(DataInput in) throws IOException { // construct matrix values = new Writable[in.readInt()][]; for (int i = 0; i < values.length; i++) { values[i] = new Writable[in.readInt()]; } // construct values for (int i = 0; i < values.length; i++) { for (int j = 0; j < values[i].length; j++) { Writable value; // construct value try { value = (Writable) valueClass.newInstance(); } catch (InstantiationException e) { throw new RuntimeException(e.toString()); } catch (IllegalAccessException e) { throw new RuntimeException(e.toString()); } value.readFields(in); // read a value values[i][j] = value; // store it in values } } }
/** Used by child copy constructors. */ protected synchronized void copy(Writable other) { if (other != null) { try { DataOutputBuffer out = new DataOutputBuffer(); other.write(out); DataInputBuffer in = new DataInputBuffer(); in.reset(out.getData(), out.getLength()); readFields(in); } catch (IOException e) { throw new IllegalArgumentException("map cannot be copied: " + e.getMessage()); } } else { throw new IllegalArgumentException("source map cannot be null"); } }
/** {@inheritDoc} */ @SuppressWarnings("unchecked") @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { id = in.readInt(); String clsName = in.readUTF(); if (clsName == null) innerSplit = in.readObject(); else { // Split wrapper only used when classes available in our classpath, so Class.forName is ok // here. Class<Writable> cls = (Class<Writable>) Class.forName(clsName); try { innerSplit = U.newInstance(cls); } catch (GridException e) { throw new IOException(e); } ((Writable) innerSplit).readFields(in); } }
public void addNextWritable(Writable next) { if (!next.getText().equals(getText())) nextWritables.add(next.getText()); }
/** Read a {@link Writable}, {@link String}, primitive type, or an array of the preceding. */ @SuppressWarnings("unchecked") public static Object readObject(DataInput in, ObjectWritable objectWritable, Configuration conf) throws IOException { String className = UTF8.readString(in); Class<?> declaredClass = PRIMITIVE_NAMES.get(className); if (declaredClass == null) { declaredClass = loadClass(conf, className); } Object instance; if (declaredClass.isPrimitive()) { // primitive types if (declaredClass == Boolean.TYPE) { // boolean instance = Boolean.valueOf(in.readBoolean()); } else if (declaredClass == Character.TYPE) { // char instance = Character.valueOf(in.readChar()); } else if (declaredClass == Byte.TYPE) { // byte instance = Byte.valueOf(in.readByte()); } else if (declaredClass == Short.TYPE) { // short instance = Short.valueOf(in.readShort()); } else if (declaredClass == Integer.TYPE) { // int instance = Integer.valueOf(in.readInt()); } else if (declaredClass == Long.TYPE) { // long instance = Long.valueOf(in.readLong()); } else if (declaredClass == Float.TYPE) { // float instance = Float.valueOf(in.readFloat()); } else if (declaredClass == Double.TYPE) { // double instance = Double.valueOf(in.readDouble()); } else if (declaredClass == Void.TYPE) { // void instance = null; } else { throw new IllegalArgumentException("Not a primitive: " + declaredClass); } } else if (declaredClass.isArray()) { // array int length = in.readInt(); instance = Array.newInstance(declaredClass.getComponentType(), length); for (int i = 0; i < length; i++) { Array.set(instance, i, readObject(in, conf)); } } else if (declaredClass == String.class) { // String instance = UTF8.readString(in); } else if (declaredClass.isEnum()) { // enum instance = Enum.valueOf((Class<? extends Enum>) declaredClass, UTF8.readString(in)); } else { // Writable Class instanceClass = null; String str = UTF8.readString(in); instanceClass = loadClass(conf, str); Writable writable = WritableFactories.newInstance(instanceClass, conf); writable.readFields(in); instance = writable; if (instanceClass == NullInstance.class) { // null declaredClass = ((NullInstance) instance).declaredClass; instance = null; } } if (objectWritable != null) { // store values objectWritable.declaredClass = declaredClass; objectWritable.instance = instance; } return instance; }