001    package com.github.sarxos.webcam.ds.ipcam;
002    
003    import java.io.File;
004    import java.util.List;
005    
006    import javax.xml.bind.JAXBContext;
007    import javax.xml.bind.JAXBException;
008    import javax.xml.bind.Unmarshaller;
009    import javax.xml.bind.annotation.XmlAccessType;
010    import javax.xml.bind.annotation.XmlAccessorType;
011    import javax.xml.bind.annotation.XmlElement;
012    import javax.xml.bind.annotation.XmlRootElement;
013    
014    import com.github.sarxos.webcam.WebcamException;
015    import com.github.sarxos.webcam.ds.ipcam.impl.IpCamDescriptor;
016    
017    
018    @XmlRootElement(name = "storage")
019    @XmlAccessorType(XmlAccessType.FIELD)
020    public class IpCamStorage {
021    
022            private static final Class<?>[] CLASSES = new Class<?>[] {
023                    IpCamStorage.class,
024                    IpCamDescriptor.class,
025            };
026    
027            private static final JAXBContext CTX;
028            static {
029                    JAXBContext c = null;
030                    try {
031                            c = JAXBContext.newInstance(CLASSES);
032                    } catch (JAXBException e) {
033                            throw new RuntimeException(e);
034                    } finally {
035                            CTX = c;
036                    }
037            }
038    
039            @XmlElement(name = "ipcam")
040            private List<IpCamDescriptor> descriptors = null;
041    
042            private transient File file = null;
043    
044            protected IpCamStorage() {
045            }
046    
047            public IpCamStorage(String file) {
048                    this(new File(file));
049            }
050    
051            public IpCamStorage(File file) {
052                    this.file = file;
053            }
054    
055            protected List<IpCamDescriptor> getDescriptors() {
056                    return descriptors;
057            }
058    
059            public void open() {
060    
061                    IpCamStorage storage = null;
062                    try {
063                            Unmarshaller unmarshaller = CTX.createUnmarshaller();
064                            storage = (IpCamStorage) unmarshaller.unmarshal(file);
065                    } catch (JAXBException e) {
066                            throw new WebcamException(e);
067                    }
068    
069                    for (IpCamDescriptor d : storage.getDescriptors()) {
070                            IpCamDeviceRegistry.register(d.getName(), d.getURL(), d.getMode(), d.getAuth());
071                    }
072            }
073    }