package org.torquebox.messaging.deployers;

import java.io.InputStream;
import java.util.Map;

import org.jboss.deployers.vfs.spi.deployer.AbstractVFSParsingDeployer;
import org.jboss.deployers.vfs.spi.structure.VFSDeploymentUnit;
import org.jboss.virtual.VirtualFile;
import org.torquebox.messaging.metadata.QueueMetaData;
import org.torquebox.messaging.metadata.QueuesMetaData;
import org.yaml.snakeyaml.Yaml;

public class QueuesYamlParsingDeployer extends AbstractVFSParsingDeployer<QueuesMetaData>{

	public QueuesYamlParsingDeployer() {
		super( QueuesMetaData.class );
		setName( "queues.yml" );
	}

	@SuppressWarnings("unchecked")
	@Override
	protected QueuesMetaData parse(VFSDeploymentUnit unit, VirtualFile file, QueuesMetaData root) throws Exception {
		InputStream in = null;
		
		QueuesMetaData queues = unit.getAttachment( QueuesMetaData.class );
		if ( queues == null ) {
			queues = new QueuesMetaData();
			unit.addAttachment( QueuesMetaData.class, queues );
		}
		
		try {
			in = file.openStream();
			Yaml yaml = new Yaml();
			Map<String, Map<String,Object>> data = (Map<String, Map<String, Object>>) yaml.load( in );
			
			for ( String queueName : data.keySet() ) {
				log.info( "Read configuration for queue [" + queueName + "]" );
				QueueMetaData queue = new QueueMetaData( queueName );
				queues.addQueue( queue );
			}
		} finally {
			if ( in != null ) {
				in.close();
			}
		}
		return null;
	}

}

@@ -452,17 +452,19 @@
  extends
- 
+%0A%09%09
 Abstract
@@ -497,16 +497,17 @@
 etaData%3E
+ 
 %7B%0A%0A%09publ
@@ -547,17 +547,16 @@
 %09%09super(
- 
 QueuesMe
@@ -563,25 +563,24 @@
 taData.class
- 
 );%0A%09%09setName
@@ -580,17 +580,16 @@
 setName(
- 
 %22queues.
@@ -592,17 +592,16 @@
 ues.yml%22
- 
 );%0A%09%7D%0A%0A%09
@@ -715,17 +715,20 @@
 le file,
- 
+%0A%09%09%09
 QueuesMe
@@ -784,18 +784,16 @@
 = null;%0A
-%09%09
 %0A%09%09Queue
@@ -822,33 +822,32 @@
 t.getAttachment(
- 
 QueuesMetaData.c
@@ -850,17 +850,16 @@
 ta.class
- 
 );%0A%09%09if 
@@ -859,17 +859,16 @@
 ;%0A%09%09if (
- 
 queues =
@@ -869,25 +869,24 @@
 eues == null
- 
 ) %7B%0A%09%09%09queue
@@ -933,17 +933,16 @@
 achment(
- 
 QueuesMe
@@ -965,18 +965,15 @@
 eues
- 
 );%0A%09%09%7D%0A
-%09%09
 %0A%09%09t
@@ -1057,16 +1057,17 @@
 %3CString,
+ 
 Object%3E%3E
@@ -1117,33 +1117,58 @@
 yaml
+%0A%09%09%09%09%09
 .load(
- 
 in
- 
 );%0A
+%0A
 %09%09%09
-%0A
+if (data != null) %7B%0A%09
 %09%09%09for (
  Str
@@ -1163,17 +1163,16 @@
 %09%09%09for (
- 
 String q
@@ -1195,25 +1195,24 @@
 keySet()
- 
 ) %7B%0A%09%09%09%09
 log.info
@@ -1207,18 +1207,26 @@
 %09%09%09%09
+%09
 log
+%0A%09%09%09%09%09%09%09
 .info(
- 
 %22Rea
@@ -1269,18 +1269,27 @@
 Name
- 
+%0A%09%09%09%09%09%09%09%09%09
 + %22%5D%22
- 
 );%0A
+%09
 %09%09%09%09
@@ -1328,17 +1328,16 @@
 etaData(
- 
 queueNam
@@ -1337,20 +1337,20 @@
 ueueName
- 
 );%0A
+%09
 %09%09%09%09queu
@@ -1365,18 +1365,22 @@
 eue(
- 
 queue
- 
 );%0A
+%09%09%09%09%7D%0A
 %09%09%09%7D
@@ -1401,17 +1401,16 @@
 %0A%09%09%09if (
- 
 in != nu
@@ -1411,17 +1411,16 @@
  != null
- 
 ) %7B%0A%09%09%09%09

Fix for TORQUE-72. Check that object is loaded and non-NULL from queues.yml.