2014-06-30 86 views
2

我正在構建一個Neo4J服務器插件。我想要一些可以在neo4j.properties或neo4j-server.properties中手動設置的配置值,然後插件可以讀取並使用這些值。我如何從ServerPlugin訪問配置值?Neo4j ServerPlugin配置

澄清: 我真的很喜歡在未來的Neo4J版本中可以使用的東西,因此屬於公共API的一部分並且不被棄用的東西是最好的。

回答

2

使用Neo4j的內部依賴機制,您可以訪問Confighttps://github.com/neo4j/neo4j/blob/master/community/kernel/src/main/java/org/neo4j/kernel/configuration/Config.java)的實例。這個類可以讓你訪問配置。

採取下列未經測試的片段作爲一個準則:

... 
import org.neo4j.kernel.configuration.Config 
... 

@Description("An extension to the Neo4j Server accessing config") 
public class ConfigAwarePlugin extends ServerPlugin 
{ 
    @Name("config") 
    @Description("Do stuff with config") 
    @PluginTarget(GraphDatabaseService.class) 
    public void sample(@Source GraphDatabaseService graphDb) { 
     Config config = ((GraphDatabaseAPI)graphDb).getDependencyResolver().resolveDependency(Config.class); 

     // do stuff with config 
    } 
} 
+0

感謝。這確實奏效,但我希望今後可能會得到支持。 1)GraphDatabaseAPI接口已被棄用與消息「這將在下一主要版本被移動到內部包」。 2)getDependencyResolver方法具有的一個消息「該方法的使用通常是建築錯誤的指示。」 3)Config.getParams方法中的代碼TODO「擺脫這一點,讓我們有更多的東西精心爲內部存儲(例如,一些可以保持元數據與屬性)。」 我很擔心這種方法的壽命。 –

0

我用Properties

Properties props = new Properties(); 
    try 
    { 
     FileInputStream in = new FileInputStream("./conf/neo4j.properties"); 
     props.load(in); 
    } 
    catch (FileNotFoundException e) 
    { 
     try 
     { 
      FileInputStream in = new FileInputStream("./neo4j.properties"); 
      props.load(in); 
     } 
     catch (FileNotFoundException e2) 
     { 
      logger.warn(e2.getMessage()); 
     } 
     catch (IOException e2) 
     { 
      logger.warn(e2.getMessage()); 
     } 
    } 
    catch (IOException e) 
    { 
     logger.warn(e.getMessage()); 
    } 
    String myPropertyString = props.getProperty("myProperty"); 
    if (myPropertyString != null) 
    { 
     myProperty = Integer.parseInt(myPropertyString); 
    } 
    else 
    { 
     myProperty = 100; 
    } 

neo4j.properties我:

...  
# Enable shell server so that remote clients can connect via Neo4j shell. 
#remote_shell_enabled=true 
# Specify custom shell port (default is 1337). 
#remote_shell_port=1234 

myProperty=100 
+0

謝謝。我有同樣的擔心,你已經部分解決了你的代碼,這是服務器啓動的地方,並嘗試從相對路徑加載neo4j.properties。 –