@Test public void test() throws Exception { PayloadCodec jsonGzipCodec = PayloadCodecFactory.getCodecByType("json,gzip"); PayloadCodec jsonCodec = PayloadCodecFactory.getCodecByType("json"); assertTrue(jsonGzipCodec instanceof PayloadCodecCompositor); String xml = IO.INSTANCE.readFrom(this.getClass().getResourceAsStream("/META-INF/dal/model/meta-codegen.xml"), "utf-8"); byte[] gzipEncoded = jsonGzipCodec.encode("topic", xml); byte[] encoded = jsonCodec.encode("topic", xml); assertTrue(gzipEncoded.length < encoded.length); String xmlDecodec = jsonGzipCodec.decode(gzipEncoded, String.class); assertEquals(xml, xmlDecodec); }
public static PayloadCodec getCodecByType(String codecString) { CodecDesc codecDesc = CodecDesc.valueOf(codecString); if (codecDesc == null) { throw new IllegalArgumentException(String.format("codec type '%s' is illegal", codecString)); } else if (codecDesc.getCompressionAlgo() == null) { return PlexusComponentLocator.lookup(PayloadCodec.class, codecString); } else { PayloadCodecCompositor cached = compositorCache.get(codecString); if (cached != null) { return cached; } else { synchronized (PayloadCodecFactory.class) { cached = compositorCache.get(codecString); if (cached == null) { cached = new PayloadCodecCompositor(codecString); cached.addPayloadCodec(PlexusComponentLocator.lookup(PayloadCodec.class, codecDesc.getCodec())); if (Codec.GZIP.equals(codecDesc.getCompressionAlgo())) { cached.addPayloadCodec(PlexusComponentLocator.lookup(PayloadCodec.class, Codec.GZIP)); } else if (Codec.DEFLATER.equals(codecDesc.getCompressionAlgo())) { int compressionLevel = 5; if (codecDesc.getLevel() != -1) { compressionLevel = codecDesc.getLevel(); } cached.addPayloadCodec(PlexusComponentLocator.lookup(PayloadCodec.class, Codec.DEFLATER + "-" + compressionLevel)); } compositorCache.put(codecString, cached); } } return cached; } } }
PayloadCodecFactory { public static PayloadCodec getCodecByType(String codecString) { CodecDesc codecDesc = CodecDesc.valueOf(codecString); if (codecDesc == null) { throw new IllegalArgumentException(String.format("codec type '%s' is illegal", codecString)); } else if (codecDesc.getCompressionAlgo() == null) { return PlexusComponentLocator.lookup(PayloadCodec.class, codecString); } else { PayloadCodecCompositor cached = compositorCache.get(codecString); if (cached != null) { return cached; } else { synchronized (PayloadCodecFactory.class) { cached = compositorCache.get(codecString); if (cached == null) { cached = new PayloadCodecCompositor(codecString); cached.addPayloadCodec(PlexusComponentLocator.lookup(PayloadCodec.class, codecDesc.getCodec())); if (Codec.GZIP.equals(codecDesc.getCompressionAlgo())) { cached.addPayloadCodec(PlexusComponentLocator.lookup(PayloadCodec.class, Codec.GZIP)); } else if (Codec.DEFLATER.equals(codecDesc.getCompressionAlgo())) { int compressionLevel = 5; if (codecDesc.getLevel() != -1) { compressionLevel = codecDesc.getLevel(); } cached.addPayloadCodec(PlexusComponentLocator.lookup(PayloadCodec.class, Codec.DEFLATER + "-" + compressionLevel)); } compositorCache.put(codecString, cached); } } return cached; } } } }
PayloadCodecFactory { public static PayloadCodec getCodecByType(String codecString) { CodecDesc codecDesc = CodecDesc.valueOf(codecString); if (codecDesc == null) { throw new IllegalArgumentException(String.format("codec type '%s' is illegal", codecString)); } else if (codecDesc.getCompressionAlgo() == null) { return PlexusComponentLocator.lookup(PayloadCodec.class, codecString); } else { PayloadCodecCompositor cached = compositorCache.get(codecString); if (cached != null) { return cached; } else { synchronized (PayloadCodecFactory.class) { cached = compositorCache.get(codecString); if (cached == null) { cached = new PayloadCodecCompositor(codecString); cached.addPayloadCodec(PlexusComponentLocator.lookup(PayloadCodec.class, codecDesc.getCodec())); if (Codec.GZIP.equals(codecDesc.getCompressionAlgo())) { cached.addPayloadCodec(PlexusComponentLocator.lookup(PayloadCodec.class, Codec.GZIP)); } else if (Codec.DEFLATER.equals(codecDesc.getCompressionAlgo())) { int compressionLevel = 5; if (codecDesc.getLevel() != -1) { compressionLevel = codecDesc.getLevel(); } cached.addPayloadCodec(PlexusComponentLocator.lookup(PayloadCodec.class, Codec.DEFLATER + "-" + compressionLevel)); } compositorCache.put(codecString, cached); } } return cached; } } }  }
PayloadCodecFactory { public static PayloadCodec getCodecByType(String codecString) { CodecDesc codecDesc = CodecDesc.valueOf(codecString); if (codecDesc == null) { throw new IllegalArgumentException(String.format("codec type '%s' is illegal", codecString)); } else if (codecDesc.getCompressionAlgo() == null) { return PlexusComponentLocator.lookup(PayloadCodec.class, codecString); } else { PayloadCodecCompositor cached = compositorCache.get(codecString); if (cached != null) { return cached; } else { synchronized (PayloadCodecFactory.class) { cached = compositorCache.get(codecString); if (cached == null) { cached = new PayloadCodecCompositor(codecString); cached.addPayloadCodec(PlexusComponentLocator.lookup(PayloadCodec.class, codecDesc.getCodec())); if (Codec.GZIP.equals(codecDesc.getCompressionAlgo())) { cached.addPayloadCodec(PlexusComponentLocator.lookup(PayloadCodec.class, Codec.GZIP)); } else if (Codec.DEFLATER.equals(codecDesc.getCompressionAlgo())) { int compressionLevel = 5; if (codecDesc.getLevel() != -1) { compressionLevel = codecDesc.getLevel(); } cached.addPayloadCodec(PlexusComponentLocator.lookup(PayloadCodec.class, Codec.DEFLATER + "-" + compressionLevel)); } compositorCache.put(codecString, cached); } } return cached; } } }  static PayloadCodec getCodecByTopicName(String topicName); static PayloadCodec getCodecByType(String codecString); }
PayloadCodecFactory { public static PayloadCodec getCodecByType(String codecString) { CodecDesc codecDesc = CodecDesc.valueOf(codecString); if (codecDesc == null) { throw new IllegalArgumentException(String.format("codec type '%s' is illegal", codecString)); } else if (codecDesc.getCompressionAlgo() == null) { return PlexusComponentLocator.lookup(PayloadCodec.class, codecString); } else { PayloadCodecCompositor cached = compositorCache.get(codecString); if (cached != null) { return cached; } else { synchronized (PayloadCodecFactory.class) { cached = compositorCache.get(codecString); if (cached == null) { cached = new PayloadCodecCompositor(codecString); cached.addPayloadCodec(PlexusComponentLocator.lookup(PayloadCodec.class, codecDesc.getCodec())); if (Codec.GZIP.equals(codecDesc.getCompressionAlgo())) { cached.addPayloadCodec(PlexusComponentLocator.lookup(PayloadCodec.class, Codec.GZIP)); } else if (Codec.DEFLATER.equals(codecDesc.getCompressionAlgo())) { int compressionLevel = 5; if (codecDesc.getLevel() != -1) { compressionLevel = codecDesc.getLevel(); } cached.addPayloadCodec(PlexusComponentLocator.lookup(PayloadCodec.class, Codec.DEFLATER + "-" + compressionLevel)); } compositorCache.put(codecString, cached); } } return cached; } } }  static PayloadCodec getCodecByTopicName(String topicName); static PayloadCodec getCodecByType(String codecString);  }