| 
									
										
										
										
											2015-11-19 14:39:21 -08:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * Copyright 2015 LinkedIn Corp. All rights reserved.
 | 
					
						
							|  |  |  |  *
 | 
					
						
							|  |  |  |  * Licensed under the Apache License, Version 2.0 (the "License");
 | 
					
						
							|  |  |  |  * you may not use this file except in compliance with the License.
 | 
					
						
							|  |  |  |  * You may obtain a copy of the License at
 | 
					
						
							|  |  |  |  *
 | 
					
						
							|  |  |  |  * http://www.apache.org/licenses/LICENSE-2.0
 | 
					
						
							|  |  |  |  *
 | 
					
						
							|  |  |  |  * Unless required by applicable law or agreed to in writing, software
 | 
					
						
							|  |  |  |  * distributed under the License is distributed on an "AS IS" BASIS,
 | 
					
						
							|  |  |  |  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
					
						
							|  |  |  |  */
 | 
					
						
							|  |  |  | package actors;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import akka.actor.UntypedActor;
 | 
					
						
							|  |  |  | import metadata.etl.EtlJob;
 | 
					
						
							|  |  |  | import models.daos.EtlJobPropertyDao;
 | 
					
						
							|  |  |  | import org.python.core.PyDictionary;
 | 
					
						
							|  |  |  | import org.python.core.PyString;
 | 
					
						
							|  |  |  | import org.python.core.PySystemState;
 | 
					
						
							|  |  |  | import org.python.util.PythonInterpreter;
 | 
					
						
							|  |  |  | import play.Logger;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import java.io.InputStream;
 | 
					
						
							|  |  |  | import java.util.Properties;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * Created by zechen on 11/8/15.
 | 
					
						
							|  |  |  |  */
 | 
					
						
							|  |  |  | public class TreeBuilderActor extends UntypedActor {
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   public PythonInterpreter interpreter;
 | 
					
						
							|  |  |  |   public PySystemState sys;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   @Override
 | 
					
						
							|  |  |  |   public void onReceive(Object o) throws Exception {
 | 
					
						
							|  |  |  |     if (o instanceof String) {
 | 
					
						
							|  |  |  |       Properties whProps = EtlJobPropertyDao.getWherehowsProperties();
 | 
					
						
							|  |  |  |       PyDictionary config = new PyDictionary();
 | 
					
						
							|  |  |  |       for (String key : whProps.stringPropertyNames()) {
 | 
					
						
							|  |  |  |         String value = whProps.getProperty(key);
 | 
					
						
							|  |  |  |         config.put(new PyString(key), new PyString(value));
 | 
					
						
							|  |  |  |       }
 | 
					
						
							|  |  |  |       sys = new PySystemState();
 | 
					
						
							|  |  |  |       sys.argv.append(config);
 | 
					
						
							|  |  |  |       interpreter = new PythonInterpreter(null, sys);
 | 
					
						
							|  |  |  |       String msg = (String) o;
 | 
					
						
							|  |  |  |       Logger.info("Start build {} tree", msg);
 | 
					
						
							|  |  |  |       InputStream in = null;
 | 
					
						
							|  |  |  |       switch (msg) {
 | 
					
						
							|  |  |  |         case "dataset":
 | 
					
						
							|  |  |  |           in = EtlJob.class.getClassLoader().getResourceAsStream("jython/DatasetTreeBuilder.py");
 | 
					
						
							|  |  |  |           break;
 | 
					
						
							|  |  |  |         case "flow":
 | 
					
						
							| 
									
										
										
										
											2016-06-13 10:15:32 -07:00
										 |  |  |           //in = EtlJob.class.getClassLoader().getResourceAsStream("jython/FlowTreeBuilder.py");
 | 
					
						
							| 
									
										
										
										
											2015-11-19 14:39:21 -08:00
										 |  |  |           break;
 | 
					
						
							|  |  |  |         default:
 | 
					
						
							| 
									
										
										
										
											2016-03-18 15:26:55 -07:00
										 |  |  |           Logger.error("unknown message : {}", msg);
 | 
					
						
							| 
									
										
										
										
											2015-11-19 14:39:21 -08:00
										 |  |  |       }
 | 
					
						
							|  |  |  |       if (in != null) {
 | 
					
						
							|  |  |  |         interpreter.execfile(in);
 | 
					
						
							|  |  |  |         in.close();
 | 
					
						
							|  |  |  |         Logger.info("Finish build {} tree", msg);
 | 
					
						
							|  |  |  |       } else {
 | 
					
						
							| 
									
										
										
										
											2016-03-18 15:26:55 -07:00
										 |  |  |         Logger.error("can not find jython script");
 | 
					
						
							| 
									
										
										
										
											2015-11-19 14:39:21 -08:00
										 |  |  |       }
 | 
					
						
							|  |  |  |     } else {
 | 
					
						
							|  |  |  |       throw new Exception("message type is not supported!");
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | }
 |